您的位置:首页 > 其它

#292 (div.2) D.Drazil and Tiles (贪心+bfs)

2015-09-25 19:54 369 查看
Description

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 Input

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

*


Hint

In the first case, there are indeed two solutions:

<>^
^*v
v<>
and

^<>
v*^
<>v
so the answer is "Not unique".


Source

Codeforces Round #292 (Div. 2)

用贪心法解决:首先应该着力填充周围只有一个空格的点,随后以它的空格为中心,再不断向外拓展,直到整个界面被填充。
注意在judge函数判断边界和‘.’时应该这样写:return (i>=0 && i<n && j>=0 && j<m && mp[i][j]=='.'); 分开判断一直错。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={0,0,-1,1};
int diry[]={-1,1,0,0};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 2006
#define inf 1e12
int n,m;
char mp

;
struct Node{
int x,y;
};
char change[]="><<>v^^v";
bool judge(int i,int j){
return (i>=0 && i<n && j>=0 && j<m && mp[i][j]=='.');
}

int nearPoint_num(int x,int y){
int ans=0;//ans表示周围的空点
for(int i=0;i<4;i++){
int tx=x+dirx[i];
int ty=y+diry[i];
if(judge(tx,ty)){
ans++;
}
}
return ans;
}

void bfs(){
queue<Node>q;
Node tmp;
Node t1,t2,t3;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(judge(i,j) && nearPoint_num(i,j)==1){
tmp.x=i;
tmp.y=j;
//printf("***%d %d\n",tmp.x,tmp.y);
q.push(tmp);
}
}
}
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=0;i<4;i++){
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(judge(t2.x,t2.y)){
mp[t1.x][t1.y]=change[i*2];
mp[t2.x][t2.y]=change[i*2+1];
//printf("%d %d %c\n",t1.x,t1.y,mp[t1.x][t1.y]);
//printf("%d %d %c\n",t2.x,t2.y,mp[t2.x][t2.y]);

for(int j=0;j<4;j++){
t3.x=t2.x+dirx[j];
t3.y=t2.y+diry[j];
if(judge(t3.x,t3.y) && nearPoint_num(t3.x,t3.y)==1){
q.push(t3);
}
}

}

}
}

int flag=1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mp[i][j]=='.'){
flag=0;
break;
}
}
if(flag==0){
break;
}
}
if(flag==0){
printf("Not unique\n");
}
else{
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%c",mp[i][j]);
}
printf("\n");
}
}

}
int main()
{
while(scanf("%d%d",&n,&m)==2){
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
}

bfs();

}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: