Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
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 拓扑排序的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- 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. ...
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial
题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...
- Codeforces Round #292 (Div. 1) C - Drazil and Park
C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...
- 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 ...
- 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 ...
随机推荐
- 使用Nexus搭建本地Maven私服
搭建了好几天这个还是不大好使,今天看了一篇文章是讲这个的,然后根据其情况,加上自己的更改最后搭建成功了 1.下载nexus, 下载地址:http://www.sonatype.org/nexus/go ...
- “”?: H3C SSH 配置+
开启ssh 服务 ssh service enable 创建用户 使用ssh local-user ssh 用户级别 authorization-attribute user-role level-1 ...
- my sql
如果改了上面的4个配置文件,要让其立即生效,可以使用如下方法 source .bash_profile . .bash_profile 基于Apache+php+mysql的许愿墙网站的搭建 方案一: ...
- By Yupei Zhang
Sparse Learning: Sparsity Learning (foundation) (1) Emergence of simple-cell receptive field propert ...
- C#函数式编程之由函数构建函数
在面向对象的编程中,如果我们需要复用其他的类,我们可以通过继承来实现.而在函数式编程中我们也可以采取不同的方式来复用这些函数.今天的教程将会讲述两种方式,其中一个就是组合,将多个函数组合成为一个函数, ...
- IIS负载均衡
工具下载链接 http://www.iis.net/downloads/microsoft/application-request-routing#additionalDownloads
- C#自学系列 - 开篇
2014年即将过去,这一年我参加了不少面试,被问到了很多问题.回来总结下发现自己确实在基础方面有着很多的不足,还有很多东西是我不知道的.遂在下半年购入书籍若干,并系统的加以学习.我目前在看的书是Jon ...
- MVC4+WebApi+Redis Session共享练习(下)
上一篇文章我们主要讲解了一些webApi和redis缓存操作,这篇文章我们主要说一些MVC相关的知识(过滤器和错误处理),及采用ajax调用webApi服务. 本篇例子采用的开发环境为:VS2010( ...
- [Java Web整合开发王者归来·刘京华] 1、 Java Web开发
目录: 1.Web技术简介 2.动态网站与静态网站 3.Java Web开发模式 4.JavaScript简介 1.Web技术简介 PS: 最近还有更凶残的技术,即整个操作系统都是基于Web的,如 ...
- [ACM_数据结构] 竞赛排名
比赛排名 Time Limit:1000MS Memory Limit:32768K Description: 欢迎参加浙江工业大学“亚信联创杯”程序设计大赛,本次竞赛采用与 ACM/ICPC 相同 ...