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. python基础语法(3)

    七.面向对象编程 python支持面向对象编程:类和对象是面向对象编程的两个主要方面,类创建一个新的类型,对象是这个类的实例. 对象可以使用普通的属于对象的变量存储数据,属于对象或类的变量被称为域:对 ...

  2. python语法快速入门(1)

    http://www.runoob.com/python/python-tutorial.html Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言 ...

  3. jQuery遮罩层效果

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. Android的项目不能直接引用可移植类库的项目解决方法

    深圳_exception() 10:25:51 Android的项目不能直接引用可移植类库的项目,但是可以引用可移植类库生成的dll,这就意味着无法直接断电调试可移植类库上海-黄药师() 10:26: ...

  5. 《JavaScript语言精粹》之函数化

    写在前面 看到好多书评和读书笔记都说<JavaScript语言精粹>字字珠玑,名不虚传..当然,要看得懂才行 其实个人认为函数化部分不是很好,举的例子不是十分恰当,之前看不懂是因为被成功误 ...

  6. sql bcp 笔记

    介绍: http://blog.csdn.net/soudog/article/details/4343415 导出格式 BCP NTS.dbo.T_User format nul -f c:/Use ...

  7. [MSSQL] Useful SQL Scripts - CalendarBase

    Useful SQL scripts DECLARE @StartDate DATETIME DECLARE @EndDate DATETIME DECLARE @FiscalBeginMonth I ...

  8. 扯扯Java中Finalization的意义

    这是Stack Overflow上关于Finalizetion意义的两段讨论,这两个观点是互为补充的. 观点1: 垃圾回收器(The garbage collector)自动在后台运行(虽然它也可以被 ...

  9. linux php环境搭建以及magento安装教程

    听朋友用magento在搭建电商系统,我好奇.遂自己下载了一个包部署了一套. 主机采用linux center os6.5,安装程序非常顺利,部署一套大概费时一个小时左右.   ########### ...

  10. atitit.跨平台gui 概览

    atitit.跨平台gui 概览 为什么需要跨平台gui 国际上那些跨平台的GUI程序,除了像Firefox之类的大型项目会重写界面外,中小型的项目基本上都是用GTK+或WxWidgets为多.毕竟要 ...