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.
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.
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.
3 3 ... .*. ...
Not unique
4 4 ..** *... *.** ....
<>** *^<> *v** <><>
2 4 *..* ....
*<>* <><>
1 1 .
Not unique
1 1 *
*
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.
解题思路:先遍历找出节点度数为1的节点,度数表示节点周围'.'的个数,放入队列中,然后填入瓷砖,并更新两点周围所有节点的度数,将度数为1的节点继续放入队列中。最后扫一遍看是否还有空位置。
代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define maxn 2000+10
int m,n;
char s[maxn][maxn];
int degree[maxn][maxn];
struct Node{
int x,y;
Node(){}
Node(int a,int b){
x =a;
y = b;
}
};
queue<Node> que;
int dx[] = {0,0,1,-1}; // right left down up
int dy[] = {1,-1,0,0};
int Count(int x,int y){
int cnt = 0;
for(int i=0;i<4;i++){
int xx = x+dx[i], yy=y+dy[i];
if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy]=='.') cnt++;
}
return cnt;
}
void update(int x,int y){
for(int i=0;i<4;i++){
int xx = x+dx[i], yy=y+dy[i];
if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy]=='.'){
degree[xx][yy] = Count(xx,yy);
if(degree[xx][yy]==1) que.push(Node(xx,yy));
}
}
}
void solve(){
Node node;
memset(degree,0,sizeof(degree));
while(!que.empty()) que.pop();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(s[i][j]=='*') continue;
degree[i][j] = Count(i,j);
if(degree[i][j]==1){
node.x = i, node.y = j;
que.push(node);
}
}
}
/* for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%d ",degree[i][j]);
}
printf("\n");
}*/
int x,y;
while(!que.empty()){
node = que.front();
que.pop();
x = node.x, y = node.y;
for(int i=0;i<4;i++){
int xx = x+dx[i], yy=y+dy[i];
if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy]=='.'){
if(i==0) s[x][y]='<',s[xx][yy]='>';
else if(i==1) s[xx][yy]='<',s[x][y]='>';
else if(i==2) s[x][y]='^',s[xx][yy]='v';
else if(i==3) s[xx][yy]='^',s[x][y]='v';
update(x,y);
update(xx,yy);
break;
}
}
}
}
void print(){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%c",s[i][j]);
}
printf("\n");
}
}
bool check(){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(s[i][j]=='.') return false;
}
}
return true;
}
int main(){
scanf("%d %d\n",&n,&m);
for(int i=0;i<n;i++){
gets(s[i]);
}
solve();
if(check()) print();
else printf("Not unique\n");
return 0;
}