您的位置:首页 > 其它

Drazil and Tiles(CodeForces 515D)

2015-08-08 15:48 351 查看
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.

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:

<>^

^*v

v<>

and

^<>

v*^

<>v

so the answer is “Not unique”.

这道题竟然能用拓扑排序来写,真是没想到,之前一味的在搜索(到现在搜索也没搞出来

代码

#include <iostream>
#include <queue>

using namespace std;

#define X first
#define Y second

typedef pair<int, int> pii;
char map[2010][2010];
int degree[2010][2010];
int n,m;
int d[4][2]={-1,0,0,-1,0,1,1,0};
queue<pii> q;

void adjust(int x, int y)
{
for(int i = 0; i < 4; i++)
{
int xx = x + d[i][0], yy = y + d[i][1];
degree[xx][yy]--;
if(degree[xx][yy] == 1) q.push({xx, yy});
}
}

void adegree()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(map[i][j] != '.')
continue;
if(j > 1 && map[i][j-1] == '.')
degree[i][j]++;
if(j < m && map[i][j+1] == '.')
degree[i][j]++;
if(i > 1 && map[i-1][j] == '.')
degree[i][j]++;
if(i < n && map[i+1][j] == '.')
degree[i][j]++;
if(degree[i][j] == 1) q.push({i, j});
}
}
}

void topo()
{
while(q.size())
{
pii qq = q.front();
q.pop();
int x = qq.X, y = qq.Y;
if(map[x-1][y] == '.')
{
map[x][y] = 'v';
map[x-1][y] = '^';
adjust(x-1, y);
}
else if(map[x+1][y] == '.')
{
map[x][y] = '^';
map[x+1][y] = 'v';
adjust(x+1, y);
}
else if(map[x][y-1] == '.')
{
map[x][y] = '>';
map[x][y-1] = '<';
adjust(x, y-1);
}
else if(map[x][y+1] == '.')
{
map[x][y] = '<';
map[x][y+1] = '>';
adjust(x, y+1);
}
else
{
if(map[x][y] == '.')
{
cout<<"Not unique"<<endl;
return ;
}
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(map[i][j] == '.')
{
cout<<"Not unique"<<endl;
return ;
}
}
}
for(int i = 1; i <= n; i++)
{
cout<<map[i]+1<<endl;
}
}

int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i++)
cin>>map[i]+1;
adegree();
topo();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: