Drazil and Tiles CodeForces - 516B (类拓扑)
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.
Examples
3 3
...
.*.
...
Not unique
4 4
..**
*...
*.**
....
<>**
*^<>
*v**
<><>
2 4
*..*
....
*<>*
<><>
1 1
.
Not unique
1 1
*
*
Note
In the first case, there are indeed two solutions:
<>^
^*v
v<>
and
^<>
v*^
<>v
so the answer is "Not unique".
题意:输入一个n*m包括'*'和'.'的矩阵,'.'表示该位置为空。'*'表示该位置已有东西。用一个1*2的瓷砖去填满空位置,如果只有一种方法,输出该方法。如果无解或有2种以上的方法输出Not unique.
题解:类似于拓扑排序的想法,将四周只有一个 ' . ' 的点放进队列,之后跑完队列里面所有的点就可以了,在跑的时候,确定另一块砖的时候要将其四周的点的b数组更新,之后队列为空后验证是否可以将所有的地都铺满
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 2e3+;
const int mod = 1e9+; int dx[] = {,,,-};
int dy[] = {,-,,};
int n,m;
char a[maxn][maxn];
int b[maxn][maxn];
struct Node {
int x,y;
};
queue<Node>que;
int Count(int x,int y) //计算每一个点的四周的.的数量
{
int ans = ;
for(int i=;i<;i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx>= && xx<n && yy>= && yy<m && a[xx][yy]=='.')
ans++;
}
return ans;
}
void update(int x,int y) //重新数四周只有一个.的点放进队列
{
for(int i=;i<;i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx>= && xx<n && yy>= && yy<m && a[xx][yy]=='.')
{
b[xx][yy]=Count(xx,yy);
if(b[xx][yy] == )
que.push(Node{xx,yy});
}
}
}
bool check() //判断是否可以将全部的.都填满
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(a[i][j] == '.')
return false;
return true;
}
int main()
{ scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%s",a[i]);
Node node;
memset(b,,sizeof b);
while(!que.empty())
que.pop();
for(int i=;i<n;i++) //将四周只有一个.的放进队列
{
for(int j=;j<m;j++)
{
if(a[i][j] == '*')
continue;
b[i][j] = Count(i,j);
if(b[i][j] == )
{
node.x = i;
node.y = j;
que.push(node);
}
}
}
while(!que.empty()) //类拓扑
{
node = que.front();
que.pop();
int x = node.x;
int y = node.y;
for (int i = ; i < ; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= && nx < n && ny >= && ny < m && a[nx][ny] == '.') {
if (i == ) {
a[x][y] = '<';
a[nx][ny] = '>';
} else if (i == ) {
a[x][y] = '>';
a[nx][ny] = '<';
} else if (i == ) {
a[x][y] = '^';
a[nx][ny] = 'v';
} else if (i == ) {
a[x][y] = 'v';
a[nx][ny] = '^';
}
update(x, y);
update(nx, ny);
break;
}
}
}
if(check())
{
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
printf("%c",a[i][j]);
}
printf("\n");
}
}
else
puts("Not unique"); }
Drazil and Tiles CodeForces - 516B (类拓扑)的更多相关文章
- CodeForces - 516B Drazil and Tiles(bfs)
https://vjudge.net/problem/CodeForces-516B 题意 在一个n*m图中放1*2或者2*1的长方形,问是否存在唯一的方法填满图中的‘.’ 分析 如果要有唯一的方案, ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- 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 516B Drazil and Tiles 其他
原文链接http://www.cnblogs.com/zhouzhendong/p/8990658.html 题目传送门 - CodeForces 516B 题意 给出一个$n\times m$的矩形 ...
- 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 516B】Drazil and Tiles
题目链接: http://codeforces.com/problemset/problem/516/B 题解: 首先可以得到一个以‘.’为点的无向图,当存在一个点没有边时,无解.然后如果这个图边双联 ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
- [ CodeForces 515 D ] Drazil and Tiles
\(\\\) \(Description\) 给出一个\(N\times M\) 的网格,一些位置是障碍,其他位置是空地,求是否存在一个用 \(1\times 2\)的骨牌铺满空地的方案,以及方案是否 ...
随机推荐
- kafka存储机制以及offset
1.前言 一个商业化消息队列的性能好坏,其文件存储机制设计是衡量一个消息队列服务技术水平和最关键指标之一.下面将从Kafka文件存储机制和物理结构角度,分析Kafka是如何实现高效文件存储,及实际应用 ...
- LINQ&EF任我行(二)--LinQ to Object
(原创:灰灰虫的家http://hi.baidu.com/grayworm)LinQ to Objects是LinQ家庭的核心,其它的LinQ也使用了与LinQ to Objects相同的查询句法.最 ...
- 【ArcGIS】文件地理数据库,个人地理数据库与ArcSDE的局别
地理数据库的类型 地理数据库是用于保存数据集集合的“容器”.有以下三种类型: 文件地理数据库 - 在文件系统中以文件夹形式存储.每个数据集都以文件形式保存,该文件大小最多可扩展至 1 TB.建议使 ...
- SQL Server2005修改计算机名后不能发布订阅
在一台安装有SQL Server2005的计算机上,更改计算机名后,在发布订阅的时候提示如下错误报告: 由于需要需要配置一个发布订阅,可是一直报告:" sql server 复制需要有实际的 ...
- [javascript]什么是闭包?
http://www.zcfy.cc/article/master-the-javascript-interview-what-is-a-closure-2127.html
- js报变量 is not a function
是变量名和函数名相同导致的 比如: function a(){} var a = a();
- 笨办法学Python(十八)
习题 18: 命名.变量.代码.函数 标题包含的内容够多的吧?接下来我要教你“函数(function)”了!咚咚锵!说到函数,不一样的人会对它有不一样的理解和使用方法,不过我只会教你现在能用到的最简单 ...
- 搭建TFTP服务器配置
实验内容: TFTP是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂,开销不大的文件传输服务.TFTP承载在UDP上,提供不可靠的数据传输服务,不提供存取授权与认 ...
- VR社交软件测试-AltspaceVR
该VR社交软件中的主界面主要分为,Events:事件:Activities:多人游戏:Words:别人创建的虚拟世界.进入游戏后可以进入场景与世界各地的人进行交谈,以虚拟3D人物的方式显示用户,具有较 ...
- express不是内部命令
有时用npm install express -g安装完express时,在写express -v会显示express不是内部命令 这样的话如果自己的安装没有问题的话就要考虑到环境变量了 win7 P ...