time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Drazil created a following problem about putting1×2tiles into ann×mgrid:
"There is a grid with some cells that are empty and some cells that are occupied. You should use1×2tiles 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 solutionwhen 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 integersnandm(1≤n,m≤2000).
The followingnlines 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 with1×2tiles. 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:
<>^^*vv<>
and
^<>v*^<>v
so the answer is "Not unique".
维护一个队列
如果此点为空且连接边的数量为1则填充它
#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<algorithm>#include<bitset>#include<climits>#include<list>#include<iomanip>#include<stack>#include<set>using namespace std;int deg[2010][2010],n,m;char pic[2010][2010];queue<pair<int,int> >qq;void add(int x,int y){if(x-1>=0){deg[x-1][y]–;if(deg[x-1][y]==1)qq.push(make_pair(x-1,y));}if(y-1>=0){deg[x][y-1]–;if(deg[x][y-1]==1)qq.push(make_pair(x,y-1));}if(x+1<n){deg[x+1][y]–;if(deg[x+1][y]==1)qq.push(make_pair(x+1,y));}if(y+1<m){deg[x][y+1]–;if(deg[x][y+1]==1)qq.push(make_pair(x,y+1));}}bool work(){for(int i=0;i<n;i++)for(int j=0;j<m;j++){if(pic[i][j]=='.'){if(i-1>=0)deg[i][j]+=pic[i-1][j]=='.'?1:0;if(j-1>=0)deg[i][j]+=pic[i][j-1]=='.'?1:0;if(i+1<n)deg[i][j]+=pic[i+1][j]=='.'?1:0;if(j+1<m)deg[i][j]+=pic[i][j+1]=='.'?1:0;if(deg[i][j]==1)qq.push(make_pair(i,j));}}while(qq.size()){pair<int,int>t=qq.front();qq.pop();if(deg[t.first][t.second]!=1||pic[t.first][t.second]!='.')continue;add(t.first,t.second);if(t.first-1>=0&&pic[t.first-1][t.second]=='.'){pic[t.first-1][t.second]='^';pic[t.first][t.second]='v';add(t.first-1,t.second);}else if(t.second-1>=0&&pic[t.first][t.second-1]=='.'){pic[t.first][t.second-1]='<';pic[t.first][t.second]='>';add(t.first,t.second-1);}else if(t.first+1<n&&pic[t.first+1][t.second]=='.'){pic[t.first][t.second]='^';pic[t.first+1][t.second]='v';add(t.first+1,t.second);}else{pic[t.first][t.second]='<';pic[t.first][t.second+1]='>';add(t.first,t.second+1);}}for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(pic[i][j]=='.')return 0;return 1;}int main(){scanf("%d%d",&n,&m);for(int i=0;i<n;i++)scanf("%s",pic[i]);if(work())for(int i=0;i<n;i++)puts(pic[i]);elseputs("Not unique");}
,每天告诉自己一次,『我真的很不错』