B. Drazil and Tiles

题目连接:

http://codeforces.com/contest/516/problem/B

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

3 3

...

.*.

...

Sample Output

Not unique

Hint

题意

给你一个nm的矩阵,你们的.位置是可以放1/*2的骨牌的,现在问你是否存在唯一解,如果有的话,输出。

否则输出Not unique

题解

有点拓扑排序的味道

找到唯一能够覆盖的,然后盖上去,找找到下一个度数为1的。

然后搞一搞就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m,d[maxn][maxn];
char s[maxn][maxn],ans[maxn][maxn];
queue<int>Qx,Qy;
bool in(int x,int y){
if(x>=1&&x<=n&&y>=1&&y<=m)return true;
return false;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
for(int j=1;j<=m;j++)
ans[i][j]=s[i][j];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(s[i][j]=='.'){
for(int k=0;k<4;k++){
int nx=i+dx[k];
int ny=j+dy[k];
if(in(nx,ny)&&s[nx][ny]=='.')
d[i][j]++;
}
}
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(d[i][j]==1)
Qx.push(i),Qy.push(j);
while(!Qx.empty()){
int x=Qx.front();
int y=Qy.front();
Qx.pop(),Qy.pop();
for(int k=0;k<4;k++){
int nx=x+dx[k];
int ny=y+dy[k];
if(in(nx,ny)&&ans[nx][ny]=='.'){
if(k==0)ans[x][y]='^',ans[nx][ny]='v';
if(k==1)ans[x][y]='v',ans[nx][ny]='^';
if(k==2)ans[x][y]='<',ans[nx][ny]='>';
if(k==3)ans[x][y]='>',ans[nx][ny]='<';
d[x][y]=d[nx][ny]=0;
for(int p=0;p<4;p++){
int nnx=nx+dx[p];
int nny=ny+dy[p];
if(in(nnx,nny)&&ans[nnx][nny]=='.')
d[nnx][nny]--;
if(d[nnx][nny]==1)
Qx.push(nnx),Qy.push(nny);
}
d[x][y]=d[nx][ny]=0;
}
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(ans[i][j]=='.'){
cout<<"Not unique"<<endl;
return 0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
printf("%c",ans[i][j]);
printf("\n");
}
}

Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序的更多相关文章

  1. Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

    传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...

  2. Codeforces Round #292 (Div. 1) - B. Drazil and Tiles

    B. Drazil and Tiles   Drazil created a following problem about putting 1 × 2 tiles into an n × m gri ...

  3. Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

    题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...

  4. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  5. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  6. Codeforces Round #292 (Div. 2) C. Drazil and Factorial

    题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...

  7. Codeforces Round #292 (Div. 1) C - Drazil and Park

    C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...

  8. Codeforces Round #292 (Div. 2) C. Drazil and Factorial 515C

    C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. Codeforces Round #285 (Div. 1) A. Misha and Forest 拓扑排序

    题目链接: 题目 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes 问题描述 L ...

随机推荐

  1. select 练习4

    21.查询score中选学多门课程的同学中分数不是所有成绩中最高分成绩的记录. select * from score  where cno in(select cno from score grou ...

  2. noip2014-day2-t2

    题意:在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条件1 ...

  3. CALayer图层的基本介绍

    掌握 ● CALayer的基本属性 ● CALayer和UIView的关系 ● position和anchorPoint的作⽤用 CALayer ● 在iOS中,你能看得见摸得着的东西基本上都是UIV ...

  4. 【转】Fiddler的基本介绍

    转:http://kb.cnblogs.com/page/130367/#basic Fiddler的官方网站:  www.fiddler2.com Fiddler的官方帮助:http://docs. ...

  5. SPOJ ONEZERO(搜索)

    搜索的好题,,,, 摘自题解: 题意: 给一个数n,求n 的最小的倍数,满足它的10进制 表示中每一位不是0就是1. 思路: 用f(x)表示被n整除取模后的最小数,那么从0开始,每次往后添0或者1,如 ...

  6. 用SQLSERVER里的bcp命令或者bulkinsert命令也可以把dat文件导入数据表

    用SQLSERVER里的bcp命令或者bulkinsert命令也可以把dat文件导入数据表 下面的内容的实验环境我是在SQLSERVER2005上面做的 之前在园子里看到两篇文章<C# 读取纯真 ...

  7. Frugalware Linux 1.9 RC1 发布

    Frugalware Linux 1.9 RC1 发布了,下载地址:fvbe-1.9rc1-full-x86_64.iso (1,874MB, SHA1). 发行通知:http://www.fruga ...

  8. 用nifi把hdfs数据导到hive

    全景图:     1. ListHDFS & FetchHDFS: ListHDFS:   FetchHDFS:   2. EvaluateJsonPath: {"status&qu ...

  9. HttpLib - 一个对 Http 协议进行封装的库

    今日,在 Codeplex 上看到一个开源项目,对 Http 协议进行了封装,这下可以方便这些在 .NET 平台下访问 Web 服务器的同学们了,比如,从 Web 服务器抓取一个页面,使用 .NET ...

  10. Http Status 参考

    http://tool.oschina.net/commons?type=5 状态码 含义 100 客户端应当继续发送请求.这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝.客户 ...