您的位置:首页 > 其它

515D - Drazil and Tiles

2015-02-24 23:40 218 查看
D. Drazil and Tiles

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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".

题意:将每一对 . 换成闭合的菱形。竖直方向是^v,水平方向是<>。如果不能确定唯一的图像则输出 “Not unique” 。

思路:

   求出每一个.上下左右的连通度数,如果为1,则该点放置的符号是唯一确定的,且与该点相邻的点放置相应的符号,将该点连通度数标记为0,然后更新相邻点的连通度数(相邻点的连通度减一)。判断是否还有不能确定的点。

#include "iostream"
#include "string"
#include "queue"
#include "algorithm"
#include "queue"
#include "string.h"
#include "cstdio"
using namespace std;

char tile[2][5] = {{"^v<>"},{"v^><"}};
int dx[] = { -1, 1, 0, 0};
int dy[] = { 0, 0, -1, 1};
char G[2010][2010];
int deg[2010][2010];
int n, m;
int cnt;
int x, y, nx, ny, tx, ty;
queue< pair<int,int> > q;

int get_deg(int x,int y)
{
int ans = 0;
if (G[x + 1][y] == '.') ans++;
if (G[x - 1][y] == '.') ans++;
if (G[x][y + 1] == '.') ans++;
if (G[x][y - 1] == '.') ans++;
return ans;
}

void show ()
{
for (int i = 1;i <= n; ++ i) {
for (int j = 1;j <= m; ++ j)
cout << G[i][j];
cout << endl;
}
//cout << "************************************" << endl;
}

bool bfs()
{
int i;
pair<int,int> tmp;
while (!q.empty()) {
tmp = q.front();
q.pop();
x = tmp.first;
y = tmp.second;
if (cnt == 0) return true;
for (i = 0;i < 4; ++ i) {
nx = x + dx[i];
ny = y + dy[i];
if (G[nx][ny] == '.') {
G[x][y] = tile[1][i];
G[nx][ny] = tile[0][i];
deg[x][y] = 0;
deg[nx][ny] = 0;
cnt -= 2;
for (int i = 0;i < 4; ++ i) {
tx = nx + dx[i];
ty = ny + dy[i];
if (G[tx][ty] == '.') {
deg[tx][ty] --;
if (deg[tx][ty] == 1)
q.push(make_pair(tx,ty));
}
}
}

}
}
if (cnt)
return false;
return true;
}

int main()
{
cin >> n >> m;
for (int i = 1;i <= n;++ i)
cin >> G[i]+1;
for (int i = 1;i <= n; ++ i) {
for (int j = 1;j <= m; ++ j) {
if (G[i][j] == '.' ) {
cnt++;
deg[i][j] = get_deg(i,j);
if (deg[i][j] == 1)
q.push(make_pair(i,j));
}
}
}
bool ok = true;
ok = bfs();
if (ok) {
show();
}
else
puts("Not unique");
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: