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
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".
题意: 铺地砖 < > ^v 两种方式 看是否铺法唯一 ,若唯一输出铺的方法 ,不唯一输出not unique
解 :统计 . 点周围 四点的 . 的个数 ,如果为1 那么说明他的排列唯一 ,那么他周围的那一个点也确定了 ,周围这个点的点数更新为0 ,与周围这个点 相临近的点 个数全部 - - ,把个数为1的点又放入队列 ,直到看所有点能不能都被访问
#include <bits/stdc++.h>
using namespace std;
int dir[4][2]= {0,1,0,-1,1,0,-1,0};//方向
char a1[4]= {'<','>','^','v'};
char a2[4]= {'>','<','v','^'};
char m1[2200][2200];//原图
int mm[2200][2200];//四周的.个数
bool v[2200][2200];
int n,m;
int go(int x,int y)
{
if(x<0) return 0;
if(x>=n) return 0;
if(y<0) return 0;
if(y>=m) return 0;
if(m1[x][y]!='.') return 0;
return 1;
}
int cnt;
pair <int,int> p;
queue<pair<int,int> >q;//只存.个数为1的位置
void bfs(int x,int y)
{
int x1,y1,kk=0;
v[x][y]=1;
for(int i=0; i<4; i++)
{
x1=x+dir[i][0];
y1=y+dir[i][1];
if(go(x1,y1))
{
kk++;
if(!v[x1][y1]) bfs(x1,y1);
}
}
mm[x][y]=kk;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int sum=0;
while(!q.empty()) q.pop();
cnt=0;
memset(mm,0,sizeof(mm));
memset(v,0,sizeof(v));
for(int i=0; i<n; i++)
{
scanf("%s",m1[i]);
for(int j=0; j<m; j++)
if(m1[i][j]=='.') cnt++;
}
if(cnt%2==1)
{
printf("Not unique\n");
continue;
}
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(m1[i][j]=='.'&&!v[i][j]) bfs(i,j);
memset(v,0,sizeof(v));
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(m1[i][j]=='.'&&mm[i][j]==1)
{
q.push(make_pair(i,j));
}
}
}
int flag=0;
while(!q.empty())
{
p=q.front();
q.pop();
int x1,y1,x2,y2;
for(int i=0; i<4; i++)
{
x1=p.first+dir[i][0];
y1=p.second+dir[i][1];
if(go(x1,y1)&&!v[x1][y1])
{
m1[p.first][p.second]=a1[i];
m1[x1][y1]=a2[i];
mm[x1][y1]=0;
v[p.first][p.second]=1;
v[x1][y1]=1;
for(int j=0; j<4; j++)
{
x2=x1+dir[j][0];
y2=y1+dir[j][1];
if(go(x2,y2))
{
mm[x2][y2]--;
if(mm[x2][y2]==1)
q.push(make_pair(x2,y2));
}
}
}
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(v[i][j]) sum++;
if(sum!=cnt) printf("Not unique\n");
else
{
for(int i=0; i<n; i++)
{
printf("%s\n",m1[i]);
}
}
}
}
//10 10
//.*.*.*.*.*
//.*.*.*.*.*
//.*.*.*.*.*
//.*.*.*.*.*
//.*.*.*.*.*
//.*.*.*.*.*
//.*.*.*.*.*
//.*.*.*.*.*
//.*.*.*....
//.*.*.*.*.*
本文探讨了一种算法,用于解决特定条件下的1x2地砖铺设问题,判断铺设方案是否唯一,并在方案唯一时输出具体铺设方式。通过统计空格周围的状态,使用广度优先搜索确保每一步操作的唯一性。
620

被折叠的 条评论
为什么被折叠?



