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.
Examples
3 3
...
.*.
...
Not unique
4 4
..**
*...
*.**
....
<>**
*^<>
*v**
<><>
2 4
*..*
....
*<>*
<><>
1 1
.
Not unique
1 1
*
*
Note
In the first case, there are indeed two solutions:
<>^
^*v
v<>
and
^<>
v*^
<>v
so the answer is "Not unique".
题意:输入一个n*m包括'*'和'.'的矩阵,'.'表示该位置为空。'*'表示该位置已有东西。用一个1*2的瓷砖去填满空位置,如果只有一种方法,输出该方法。如果无解或有2种以上的方法输出Not unique.
题解:类似于拓扑排序的想法,将四周只有一个 ' . ' 的点放进队列,之后跑完队列里面所有的点就可以了,在跑的时候,确定另一块砖的时候要将其四周的点的b数组更新,之后队列为空后验证是否可以将所有的地都铺满
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<sstream> #include<cmath> #include<stack> #include<map> #include<cstdlib> #include<vector> #include<string> #include<queue> using namespace std; #define ll long long #define llu unsigned long long #define INF 0x3f3f3f3f const double PI = acos(-1.0); const int maxn = 2e3+10; const int mod = 1e9+7; int dx[] = {0,0,1,-1}; int dy[] = {1,-1,0,0}; int n,m; char a[maxn][maxn]; int b[maxn][maxn]; struct Node { int x,y; }; queue<Node>que; int Count(int x,int y) //计算每一个点的四周的.的数量 { int ans = 0; for(int i=0;i<4;i++) { int xx = x + dx[i]; int yy = y + dy[i]; if(xx>=0 && xx<n && yy>=0 && yy<m && a[xx][yy]=='.') ans++; } return ans; } void update(int x,int y) //重新数四周只有一个.的点放进队列 { for(int i=0;i<4;i++) { int xx = x + dx[i]; int yy = y + dy[i]; if(xx>=0 && xx<n && yy>=0 && yy<m && a[xx][yy]=='.') { b[xx][yy]=Count(xx,yy); if(b[xx][yy] == 1) que.push(Node{xx,yy}); } } } bool check() //判断是否可以将全部的.都填满 { for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(a[i][j] == '.') return false; return true; } int main() { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",a[i]); Node node; memset(b,0,sizeof b); while(!que.empty()) que.pop(); for(int i=0;i<n;i++) //将四周只有一个.的放进队列 { for(int j=0;j<m;j++) { if(a[i][j] == '*') continue; b[i][j] = Count(i,j); if(b[i][j] == 1) { node.x = i; node.y = j; que.push(node); } } } while(!que.empty()) //类拓扑 { node = que.front(); que.pop(); int x = node.x; int y = node.y; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] == '.') { if (i == 0) { a[x][y] = '<'; a[nx][ny] = '>'; } else if (i == 1) { a[x][y] = '>'; a[nx][ny] = '<'; } else if (i == 2) { a[x][y] = '^'; a[nx][ny] = 'v'; } else if (i == 3) { a[x][y] = 'v'; a[nx][ny] = '^'; } update(x, y); update(nx, ny); break; } } } if(check()) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { printf("%c",a[i][j]); } printf(" "); } } else puts("Not unique"); }