CF_292_D_ Drazil and Tiles_贪心、dfs
2015-02-18 12:45
369 查看
一步两步,一步两步,一步一步似爪牙,是魔鬼的步伐。
题意:
隔壁老王又想了个题目,在一个n*m的方阵里,*表示有障碍物,.表示空白,要求用1*2的方块填满方阵,且没有方块重叠。如果无法填满或者有多重方法,输出Not unique,否则用<>、^v方式输出填满的方式。
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.
贪心就好,从只有一种方法的点开始,用dfs遍历,一开始用粗犷的遍历法,结果超时,用dfs的话就有顺序很多。
代码如下:
题意:
隔壁老王又想了个题目,在一个n*m的方阵里,*表示有障碍物,.表示空白,要求用1*2的方块填满方阵,且没有方块重叠。如果无法填满或者有多重方法,输出Not unique,否则用<>、^v方式输出填满的方式。
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.
贪心就好,从只有一种方法的点开始,用dfs遍历,一开始用粗犷的遍历法,结果超时,用dfs的话就有顺序很多。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> using namespace std; #define mxn 2010 int n,m; char a[mxn][mxn]; int mx[]={0,1,0,-1},my[]={1,0,-1,0}; bool okk(int x,int y){ if(x<0||y<0||x>=n||y>=m) return false; return a[x][y]=='.'; } void dfs(int x,int y){ if(!okk(x,y)) return; int tem=0,rec; for(int i=0;i<4;++i) if(okk(x+mx[i],y+my[i])){ ++tem; rec=i; } if(!tem) return; if(tem==1){ if(rec==0){ a[x][y]='<'; a[x][y+1]='>'; } else if(rec==1){ a[x][y]='^'; a[x+1][y]='v'; } else if(rec==2){ a[x][y]='>'; a[x][y-1]='<'; } else{ a[x][y]='v'; a[x-1][y]='^'; } for(int i=0;i<4;++i) dfs(x+mx[rec]+mx[i],y+my[rec]+my[i]); } } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ for(int i=0;i<n;++i) scanf("%s",a[i]); for(int i=0;i<n;++i) for(int j=0;j<m;++j) dfs(i,j); bool ok=true; for(int i=0;i<n;++i){ for(int j=0;j<m;++j) if(a[i][j]=='.'){ ok=false; break; } if(!ok) break; } if(!ok) puts("Not unique"); else for(int i=0;i<n;++i) printf("%s\n",a[i]); } return 0; }
相关文章推荐
- #292 (div.2) D.Drazil and Tiles (贪心+bfs)
- codeforces#292-D. Drazil and Tiles-暴力/拓扑排序
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial(贪心YY)
- D. Drazil and Tiles (CF 515D bfs搜索)
- CF_292_B_Drazil and His Happy Friends_数学
- Codeforces Round #292 (Div. 2)D. Drazil and Tiles
- CF_292_C_Drazil and Factorial_数学
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
- Codeforces Round #292 (Div. 1) - B. Drazil and Tiles
- Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]
- CF 525C(Ilya and Sticks-贪心)
- CF:322D - Ciel and Duel 贪心 或者 DP 我用的贪心 。。难道sort跟qsort是不一样的么?
- CF Drazil and His Happy Friends
- CF - 782D. Innokenty and a Football League - 模拟+思维+贪心+dfs插入
- Codeforces Round #292 (Div. 2) -- C. Drazil and Factorial
- CF 518E(Arthur and Questions-贪心)
- #292 (div.2) D.Drazil and Tiles
- Codeforces Round #292 (Div. 2) -- A. Drazil and Date
- 【CF】D. Arthur and Walls(BFS + 贪心)
- CF Drazil and Factorial (打表)