您的位置:首页 > 其它

Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)

2017-09-19 21:06 549 查看
D. Drazil and Tiles

time limit per test
 2 seconds

memory limit per test
 256 megabytes

input
 standard input

output
 standard output

Drazil created a following problem about putting 1 × 2 tiles into an n × m grid:

"There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tiles should cover
each other. And you should print a solution about how to do it."

But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique?
Otherwise contestant may print 'Not unique' ".

Drazil found that the constraints for this task may be much larger than for the original task!

Can you solve this new problem?

Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 2000).

The following n lines describe the grid rows. Character '.'
denotes an empty cell, and the character '*' denotes a cell that is occupied.

Output

If there is no solution or the solution is not unique, you should print the string "Not unique".

Otherwise you should print how to cover all empty cells with 1 × 2 tiles. Use characters "<>"
to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.

Sample test(s)

input
3 3
...
.*.
...


output
Not unique


input
4 4
..**
*...
*.**
....


output
<>**
*^<>
*v**
<><>


input
2 4
*..*
....


output
*<>*
<><>


input
1 1
.


output
Not unique


input
1 1
*


output
*


Note

In the first case, there are indeed two solutions:
<>^
^*v
v<>


and
^<>
v*^
<>v


so the answer is "Not unique".

思路:用有点类似拓扑排序的方法做。。一直找度为1的结点,直到找不到为止,这里的度的意思是'.'挨着的'.'的个数



#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 2005;
char a[maxn][maxn];
pair<int ,int> p[maxn * maxn];
int mox[4] = {-1, 0, 1, 0};
int moy[4] = {0, 1, 0, -1};
char cg[4] = {'v', '<', '^', '>'};
int n, m;

bool ispoint(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && a[x][y] == '.');
}

int deg(int x, int y) {
int ans = 0;
for(int i = 0; i < 4; i++) {
int fx = x + mox[i];
int fy = y + moy[i];
if(ispoint(fx, fy)) ans++;
}
return ans;
}

int main() {
while(scanf("%d %d", &n, &m) != EOF) {
for(int i = 0; i < n; i++)
scanf("%s", a[i]);

int h = 0, sum = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(ispoint(i, j) && deg(i, j) == 1)
p[sum++] = make_pair(i, j);

for(; h < sum; h++) {
int x = p[h].first, y = p[h].second;
for(int i = 0; i < 4; i++) {
int fx = x + mox[i];
int fy = y + moy[i];
if(ispoint(fx, fy)) {
a[x][y] = cg[i];
a[fx][fy] = cg[i ^ 2];
for(int i = 0; i < 4; i++) {
int ffx = fx + mox[i];
int ffy = fy + moy[i];
if(ispoint(ffx, ffy) && deg(ffx, ffy) == 1)
p[sum++] = make_pair(ffx, ffy);
}
break;
}
}
}

int flag = 0;
for(int i = 0; i < n && !flag; i++)
for(int j = 0; j < m && !flag; j++)
if(a[i][j] == '.' ) flag = 1;
if(flag == 1) printf("Not unique\n");
else {
for(int i = 0; i < n; i++, printf("\n"))
for(int j = 0; j < m; j++)
printf("%c", a[i][j]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: