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的<>或者是^v,使得铺满所有的点。
如果解不唯一,或者是无解,输出Not unique.
思路:
1、思路来源:http://blog.csdn.net/highmath_final/article/details/47358981
2、如果图小一点的话,跑二分图匹配即可~没想到这种题还可以用类拓扑排序的思维方式来解,真的蛮不错的一个模型。
我们这里定义一个顶点的度就是其周围点的个数。
那么对应度为1的点是一定确定了放置这个点的方式的,对应这个点的四周四个位子中,此时一定只有一个点,那么对应将此处和那个位子直接放置这个1*2的形状即可。
(假设这个点在这个度为1的点的下边,那么直接放置^v即可。)
然后处理完这个点之后,对应接下来进行拆边的操作,如果再发现了度为1的点,入队.....
一直处理下去这样的类拓扑排序的过程即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int x,y;
} now,nex;
char a[2002][2002];
int degree[2002][2002];
int fx[4]= {0,0,1,-1};
int fy[4]= {1,-1,0,0};
int n,m;
void Getdegree()
{
memset(degree,0,sizeof(degree));
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
for(int k=0; k<4; k++)
{
int xx=i+fx[k];
int yy=j+fy[k];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
{
if(a[xx][yy]=='.')
{
degree[i][j]++;
}
}
}
}
}
}
void Top_Sort()
{
queue<node >s;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(degree[i][j]==1&&a[i][j]=='.')
{
now.x=i;
now.y=j;
s.push(now);
}
}
}
while(!s.empty())
{
now=s.front();
s.pop();
int x=now.x;
int y=now.y;
if(a[x+1][y]=='.')
{
a[x][y]='^';
a[x+1][y]='v';
for(int z=0; z<4; z++)
{
int xx=x+1+fx[z];
int yy=y+fy[z];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
if(degree[xx][yy]--)
{
if(degree[xx][yy]==1&&a[xx][yy]=='.')
{
now.x=xx;
now.y=yy;
s.push(now);
}
}
}
}
else if(a[x-1][y]=='.')
{
a[x][y]='v';
a[x-1][y]='^';
for(int z=0; z<4; z++)
{
int xx=x-1+fx[z];
int yy=y+fy[z];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
if(degree[xx][yy]--)
{
if(degree[xx][yy]==1&&a[xx][yy]=='.')
{
now.x=xx;
now.y=yy;
s.push(now);
}
}
}
}
else if(a[x][y+1]=='.')
{
a[x][y]='<';
a[x][y+1]='>';
for(int z=0; z<4; z++)
{
int xx=x+fx[z];
int yy=y+1+fy[z];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
if(degree[xx][yy]--)
{
if(degree[xx][yy]==1&&a[xx][yy]=='.')
{
now.x=xx;
now.y=yy;
s.push(now);
}
}
}
}
else if(a[x][y-1]=='.')
{
a[x][y]='>';
a[x][y-1]='<';
for(int z=0; z<4; z++)
{
int xx=x+fx[z];
int yy=y-1+fy[z];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
if(degree[xx][yy]--)
{
if(degree[xx][yy]==1&&a[xx][yy]=='.')
{
now.x=xx;
now.y=yy;
s.push(now);
}
}
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='.')
{
printf("Not unique\n");
return ;
}
}
}
for(int i=0;i<n;i++)
{
printf("%s\n",a[i]);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
{
scanf("%s",a[i]);
}
Getdegree();
Top_Sort();
}
return 0;
}