cf Round #292 (Div. 2)D. Drazil and Tiles 构造
来源:互联网 发布:数据库访问框架 编辑:程序博客网 时间:2024/05/29 06:54
需要将n*m的平面铺上1*2的砖,没有解或有多解输出Not unique,有一个解输出这个解。
暴力肯定不行,倘若每个点都有>=2度,则肯定有多解,若有点'.',且度数为0,肯定无解。维护队列,将度数为1的加入,铺一块并更新周围cell的度数。
#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<cmath>#include<iomanip>#include<map>#include<algorithm>#include<queue>#include<set>#define inf 10000000#define pi acos(-1.0)#define eps 1e-8#define seed 131using namespace std;typedef pair<int,int> pii;typedef unsigned long long ULL;typedef long long LL;const int maxn=100005;char g[2005][2005];int n,m;int d[2005][2005];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};char rt[4][2];bool isin(int x,int y){ if(x>=0&&x<n&&y>=0&&y<m) return true; return false;}void get(int a,int b){ int x,y; for(int i=0;i<4;i++) { x=a+dir[i][0]; y=b+dir[i][1]; if(isin(x,y)&&g[x][y]=='.') d[a][b]++; }}int main(){ rt[0][0]='^';rt[0][1]='v'; rt[1][0]='v';rt[1][1]='^'; rt[2][0]='<';rt[2][1]='>'; rt[3][0]='>';rt[3][1]='<'; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",g[i]); memset(d,0,sizeof(d)); queue<pii>que; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(g[i][j]=='.') { get(i,j); if(d[i][j]==1) que.push(pii(i,j)); if(d[i][j]==0) { printf("Not unique\n"); return 0; } } } } while(!que.empty()) { pii p=que.front(); que.pop(); if(d[p.first][p.second]==1) { int x,y; d[p.first][p.second]=0; for(int i=0;i<4;i++) { if(isin(p.first+dir[i][0],p.second+dir[i][1])) if(d[p.first+dir[i][0]][p.second+dir[i][1]]>=1) { g[p.first][p.second]=rt[i][0]; g[p.first+dir[i][0]][p.second+dir[i][1]]=rt[i][1]; x=p.first+dir[i][0];y=p.second+dir[i][1]; d[x][y]=0; break; } } for(int i=0;i<4;i++) { if(isin(p.first+dir[i][0],p.second+dir[i][1])) if(d[p.first+dir[i][0]][p.second+dir[i][1]]>=1) { d[p.first+dir[i][0]][p.second+dir[i][1]]--; if(d[p.first+dir[i][0]][p.second+dir[i][1]]==0&&g[p.first+dir[i][0]][p.second+dir[i][1]]=='.') { printf("Not unique\n"); return 0; } if(d[p.first+dir[i][0]][p.second+dir[i][1]]==1) { que.push(pii(p.first+dir[i][0],p.second+dir[i][1])); } } } for(int i=0;i<4;i++) { if(isin(x+dir[i][0],y+dir[i][1])) if(d[x+dir[i][0]][y+dir[i][1]]>=1) { d[x+dir[i][0]][y+dir[i][1]]--; if(d[x+dir[i][0]][y+dir[i][1]]==0&&g[x+dir[i][0]][y+dir[i][1]]=='.') { printf("Not unique\n"); return 0; } if(d[x+dir[i][0]][y+dir[i][1]]==1) { que.push(pii(x+dir[i][0],y+dir[i][1])); } } } } } bool flag=true; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(d[i][j]!=0) { flag=false; break; } } if(!flag) break; } if(flag) { for(int i=0;i<n;i++) printf("%s\n",g[i]); } else printf("Not unique\n"); return 0;}
0 0
- cf Round #292 (Div. 2)D. Drazil and Tiles 构造
- Codeforces Round #292 (Div. 2)D. Drazil and Tiles
- #292 (div.2) D.Drazil and Tiles
- Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)
- Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)
- 【CF 515D】 Drazil and Tiles
- D. Drazil and Tiles (CF 515D bfs搜索)
- Codeforces Round #291 (Div. 1)B. Drazil and Tiles
- codeforces 515D Drazil and Tiles(构造)
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓扑排序)
- codeforces#292-D. Drazil and Tiles-暴力/拓扑排序
- Codeforces Round #292 (Div. 2) -- A. Drazil and Date
- Codeforces Round #292 (Div. 2) -- C. Drazil and Factorial
- Codeforces Round #292 (Div. 2) C. Drazil and Factoria
- Codeforces Round #292 (Div. 2) Problem C - Drazil and Factorial
- Codeforces Round #292 (Div. 2) -- A. Drazil and Date
- [Codeforces515D]Drazil and Tiles(构造)
- codeforces 515D Drazil and Tiles
- 关于VM_COPYDATA的疑惑
- Cocos2d-x 3.0final-《微信飞机大战》实现 -----沈大海
- 例题10-3 选择与除法 UVa10375
- [转]Android开发中常用工具类总结
- 英特尔成为了对Linux贡献最大的公司
- cf Round #292 (Div. 2)D. Drazil and Tiles 构造
- 安卓学习之数据存储之以文件方式存储
- 《Linus Torvalds自传》摘录
- C# WinForm中的SuspendLayout、ResumeLayout、PerformLayou
- Vim as IDE
- Leetcode NO.47 Permutations II
- Queue Team
- What Are Stakeholders – How To Identify Them?
- 在ASP.NET应用程序里执行定时器(Timer),运行于服务器端