Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]
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 use1 × 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 solutionwhen 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.
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.
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 with1 × 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.
3 3
...
.*.
...
Not unique
4 4
..**
*...
*.**
....
<>**
*^<>
*v**
<><>
2 4
*..*
....
*<>*
<><>
1 1
.
Not unique
1 1
*
*
In the first case, there are indeed two solutions:
<>^
^*v
v<>
and
^<>
v*^
<>v
so the answer is "Not unique".
题意及题解转自田神:http://blog.csdn.net/tc_to_top/article/details/43876015
题目大意:n*m的矩阵,' . '表示位置空,' * '表示障碍物,问能不能用尖括号填满空的点,使矩阵中所有的尖括号都两两配对,水平配对:<> 竖直配对:^v,若不存在或答案不唯一输出Not unique
题目分析:有趣的题,DFS搜索,策略:先把*的点的数量记下来,每次向四周扩展时先找只有一个方向可扩展的点扩展,因为它的灵活度最小,也就是说在它这的策略是唯一的,每个点都搜一次,最后如果n * m = cnt表示每个点都填满了(包括障碍物)则说明有且只有一解
这题还可以用拓扑排序,而不是dfs,第一份代码是我的拓扑排序,第二份是田神的dfs,结果dfs还要快,晕= =
9954592 | 2015-02-22 04:53:19 | njczy2010 | D - Drazil and Tiles | GNU C++ | Accepted | 233 ms | 26400 KB |
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 2010
#define M 10005
//#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define ull unsigned long long
#define LL long long
#define eps 1e-6
//#define inf 2147483647
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int m;
char s[N][N];
int r[N][N];
int dirx[]={,,-,}; //d,r,u,l
int diry[]={,,,-};
int tot; typedef struct
{
int x;
int y;
}PP; int calr(int x,int y)
{
r[x][y]=;
int dir;
if(s[x][y]!='.') return -;
int i;
int nx,ny;
for(i=;i<;i++){
nx=x+dirx[i];
ny=y+diry[i];
if(s[nx][ny]=='.'){
r[x][y]++;
dir=i;
}
}
return dir;
} void ini()
{
int i,j;
memset(r,,sizeof(r));
for(i=;i<=n+;i++){
for(j=;j<=m+;j++){
s[i][j]=;
}
}
for(i=;i<=n;i++){
scanf("%s",s[i]+);
}
tot=;
} void solve()
{
queue<PP>q;
int i,j;
PP te,nt,ntt;
int dir;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
if(s[i][j]=='*'){
tot++;continue;
}
calr(i,j);
te.x=i;te.y=j;
if(r[i][j]==){
q.push(te);
}
}
}
while(q.size()>=)
{
te=q.front();
q.pop();
dir=calr(te.x,te.y);
if(r[te.x][te.y]!=) continue;
nt.x=te.x+dirx[ dir ];
nt.y=te.y+diry[ dir ];
tot+=;
if(dir==){
s[te.x][te.y]='^';s[nt.x][nt.y]='v';
}
else if(dir==){
s[te.x][te.y]='v';s[nt.x][nt.y]='^';
}
else if(dir==){
s[te.x][te.y]='<';s[nt.x][nt.y]='>';
}
else if(dir==){
s[te.x][te.y]='>';s[nt.x][nt.y]='<';
}
for(j=;j<;j++){
ntt.x=nt.x+dirx[j];
ntt.y=nt.y+diry[j];
calr(ntt.x,ntt.y);
if(r[ntt.x][ntt.y]==){
q.push(ntt);
}
}
}
} void out()
{
if(tot!=n*m){
printf("Not unique\n");
}
else{
int i;
for(i=;i<=n;i++){
printf("%s\n",s[i]+);
}
}
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%d%d",&n,&m)!=EOF)
{
ini();
solve();
out();
}
return ;
}
下面转一下田神的dfs:
9954596 | 2015-02-22 04:54:37 | njczy2010 | D - Drazil and Tiles | GNU C++ | Accepted | 155 ms | 4056 KB |
#include <cstdio>
#include <cstring>
int const MAX = ;
char s[MAX][MAX];
int n, m, cnt;
int dx[] = {, , , -};
int dy[] = {, -, , }; void dfs(int x, int y)
{
if(x < || x > n || y < || y > m || s[x][y] != '.')
return;
int dir = -, sum = ;
for(int i = ; i < ; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(s[xx][yy] == '.')
{
sum ++;
dir = i;
}
}
if(sum == ) //保证解的唯一性
{
if(dir == )
{
s[x][y] = '<';
s[x][y + ] = '>';
}
else if(dir == )
{
s[x][y] = '>';
s[x][y - ] = '<';
}
else if(dir == )
{
s[x][y] = '^';
s[x + ][y] = 'v';
}
else if(dir == )
{
s[x][y] = 'v';
s[x - ][y] = '^';
}
cnt += ;
for(int i = ; i < ; i++)
dfs(x + dx[dir] + dx[i], y + dy[dir] + dy[i]);
}
} int main()
{
cnt = ;
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++)
scanf("%s", s[i] + );
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
if(s[i][j] == '*')
cnt ++;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
dfs(i, j);
if(cnt == n * m)
for(int i = ; i <= n; i++)
printf("%s\n", s[i] + );
else
printf("Not unique\n");
}
Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]的更多相关文章
- 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. 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 ...
随机推荐
- HTML的历史与历史遗留问题
1. <style type="text/css"> 从前,HTML的设计者认为以后应该还会有其他样式,不过如今我们已经醒悟,事实表明,完全可以只使用<style ...
- http://www.360doc.com/content/10/0928/12/11991_57014502.shtml
http://www.360doc.com/content/10/0928/12/11991_57014502.shtml
- How `delete’ works ?
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=21 February 16, 2013 How `delete’ works ...
- 30行代码消费腾讯人工智能开放平台提供的自然语言处理API
腾讯人工智能AI开放平台上提供了很多免费的人工智能API,开发人员只需要一个QQ号就可以登录进去使用. 腾讯人工智能AI开放平台的地址:https://ai.qq.com/ 里面的好东西很多,以自然语 ...
- Django请求,响应,ajax以及CSRF问题
二.request对象常用属性: Attribute Description path 请求页面的全路径,不包括域名端口参数.例如: /users/index method 一个全大写的字符串,表示请 ...
- iview table 已选项的数据 this.$refs.tables.$refs.tablesMain.getSelection()
iview table 已选项的数据 this.$refs.tables.$refs.tablesMain.getSelection() 由于我这里table组件是套了两层组件 所以是进入了两个层次拿 ...
- Mysql 访问远程数据库,报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost
参考:http://www.cnblogs.com/xyzdw/archive/2011/08/11/2135227.html 解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在loc ...
- eclipse修改SVN账号密码
首先,eclipse中打开window------>preferences------->SVN页面,如下所示: 注意我圈起来的地方,JavaHL是不可用的,因此SVN接口应该是SVNKi ...
- vue 父子组件的加载顺序
一.加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount ...
- 利用python库twilio来免费发送短信
大家好,我是四毛,最近开通了个人公众号“用Python来编程”,欢迎大家“关注”,这样您就可以收到优质的文章了. 今天跟大家分享的主题是利用python库twilio来免费发送短信. 先放一张成品图 ...