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. 2004FBI树

    题目描述 Description 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树[1],它的结点类型 ...

  2. netbeans中实体类代码的bug

    用了netbeans中实体类代码后,忽然报错: com.sun.tools.javac.code.Symbol$CompletionFailure: 找不到sun.util.logging.Platf ...

  3. WPF RichTextBox,关键字搜索,样式改变,超链接替换,图文混排

    RichTextBox 只是一个控件,表示对 FlowDocument 对象执行操作的丰富编辑控件.它所承载的内容由其 Document 属性来呈现. Document 是一个 FlowDocumen ...

  4. Windows Phone 8.1 新特性 - 控件之应用程序栏

    2014年4月3日的微软Build 2014 大会上,Windows Phone 8.1 正式发布.相较于Windows Phone 8,不论从用户还是开发者的角度,都产生了很大的变化.接下来我们会用 ...

  5. 5.4-5.8webstorm css

    关于使用css3 动画完成牛顿摆球效果: 使用的规则为@keyframes,不过目前的浏览器都不支持该规则. Firefox 支持替代的 @-moz-keyframes 规则. Opera 支持替代的 ...

  6. [数据结构] N皇后问题

    代码: #include <iostream> #include <string.h> #include <algorithm> using namespace s ...

  7. 【6_100】Same Tree

    Same Tree Total Accepted: 97481 Total Submissions: 230752 Difficulty: Easy Given two binary trees, w ...

  8. 关于hr标签兼容个浏览器的代码

    hr标签,相信大家都能熟悉,我们一般用它来产生横线的效果.我们可以对它定义“颜色”.“高度”.“宽度”.“边框”等样式. 在此只讨论“颜色”和“边框”对于不同版本浏览器的兼容性. 颜色: 火狐.IE7 ...

  9. 20145301&20145321&20145335实验一

    这次实验我的组员为:20145301赵嘉鑫.20145321曾子誉.20145335郝昊 实验内容详见:实验一报告

  10. 【原创】注册assembly 到GAC

    http://msdn.microsoft.com/en-us/library/dkkx7f79.aspx http://stackoverflow.com/questions/2182316/how ...