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 *
*
题意:问是否只有一种将空白部分用1*2的砖铺满的方法,如果只有一种,输出铺砖的结果。
思路:将周围有三个不能铺砖的点的空白点放入队列,那么这个点的铺砖方式就确定了,同时重新搜索周围的点,直到队列为空。最后若能铺满,则答案唯一。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
int T,t,n,m,sum;
char s[2010][2010];
bool vis[2010][2010];
bool flag;
queue<int> qu;
void find(int i,int j)
{
int k;
k=vis[i-1][j]+vis[i+1][j]+vis[i][j-1]+vis[i][j+1];
if(k==4)
{
flag=true;
return;
}
else if(k==3)
qu.push(i*3000+j);
}
void solve2(int i,int j)
{
if(vis[i-1][j]==0)
find(i-1,j);
if(vis[i+1][j]==0)
find(i+1,j);
if(vis[i][j-1]==0)
find(i,j-1);
if(vis[i][j+1]==0)
find(i,j+1);
}
void debug()
{
for(int i=0;i<=n+1;i++)
printf("%s\n",s[i]);
}
void solve()
{
int i,j,k;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(i=0;i<=n+1;i++)
s[i][0]=s[i][m+1]='*';
for(i=0;i<=m+1;i++)
s[0][i]=s[n+1][i]='*';
for(i=0;i<=n+1;i++)
for(j=0;j<=m+1;j++)
if(s[i][j]=='*')
{
vis[i][j]=1;
sum++;
}
sum=sum-n*2-m*2-4;
if((n*m-sum)&1)
{
flag=true;
return;
}
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(vis[i][j]==0)
{
k=vis[i-1][j]+vis[i+1][j]+vis[i][j-1]+vis[i][j+1];
if(k==4)
{
flag=true;
return;
}
else if(k==3)
qu.push(i*3000+j);
}
while(!qu.empty())
{
if(flag)
return;
k=qu.front();
qu.pop();
i=k/3000;j=k%3000;
if(vis[i][j])
continue;
if(vis[i-1][j]==0)
{
vis[i][j]=vis[i-1][j]=1;
s[i][j]='v';s[i-1][j]='^';
solve2(i,j);solve2(i-1,j);
}
else if(vis[i+1][j]==0)
{
vis[i][j]=vis[i+1][j]=1;
s[i][j]='^';s[i+1][j]='v';
solve2(i,j);solve2(i+1,j);
}
else if(vis[i][j-1]==0)
{
vis[i][j]=vis[i][j-1]=1;
s[i][j]='>';s[i][j-1]='<';
solve2(i,j);solve2(i,j-1);
}
else if(vis[i][j+1]==0)
{
vis[i][j]=vis[i][j+1]=1;
s[i][j]='<';s[i][j+1]='>';
solve2(i,j);solve2(i,j+1);
}
sum+=2;
}
if(sum!=n*m)
flag=true;
}
int main()
{
int i,j,k;
flag=false;
solve();
if(flag)
printf("Not unique\n");
else
{
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
printf("%c",s[i][j]);
printf("\n");
}
}
}