Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓扑排序)
来源:互联网 发布:知乎女神王珏 编辑:程序博客网 时间:2024/05/18 00:19
题目地址:codeforces 292 B
用队列维护度数为1的点,也就是可以唯一确定的点,然后每次找v1,v2,并用v2来更新与之相连的点,如果更新后的点度数为1,就加入队列。若最后还有为”.”的,说明无解或解不唯一。
代码如下:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")#define LL __int64#define pi acos(-1.0)const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;int jx[]={0,0,1,-1};int jy[]={1,-1,0,0};int d[5002][5002], n, m;char mp[5002][5002];struct node{ int x, y;};queue<node>q;bool topo(){ int i, j, k, x, y, a, b, h; node f1, f2; while(!q.empty()){ f1=q.front(); q.pop(); if(!d[f1.x][f1.y]) continue ; d[f1.x][f1.y]=0; for(k=0;k<4;k++){ x=f1.x+jx[k]; y=f1.y+jy[k]; if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]=='.'){ d[x][y]--; if(k==0){ mp[f1.x][f1.y]='<'; mp[x][y]='>'; } else if(k==1){ mp[f1.x][f1.y]='>'; mp[x][y]='<'; } else if(k==2){ mp[f1.x][f1.y]='^'; mp[x][y]='v'; } else{ mp[f1.x][f1.y]='v'; mp[x][y]='^'; } for(h=0;h<4;h++){ a=x+jx[h]; b=y+jy[h]; if(a>=0&&a<n&&b>=0&&b<m&&mp[a][b]=='.'){ d[a][b]--; if(d[a][b]==1){ f2.x=a;f2.y=b; q.push(f2); } } } } } } for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(mp[i][j]=='.') return 0; } } return 1;}int main(){ int i, j, x, y, cnt, k, ans; while(scanf("%d%d",&n,&m)!=EOF){ for(i=0;i<n;i++){ scanf("%s",mp[i]); } node f; for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(mp[i][j]!='.') continue ; cnt=0; for(k=0;k<4;k++){ x=i+jx[k]; y=j+jy[k]; if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]=='.') cnt++; } d[i][j]=cnt; if(cnt==1){ f.x=i;f.y=j; q.push(f); } } } if(topo()){ for(i=0;i<n;i++){ printf("%s\n",mp[i]); } } else puts("Not unique"); } return 0;}
1 0
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓扑排序)
- Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)
- Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)
- Codeforces Round #291 (Div. 1)B. Drazil and Tiles
- codeforces#292-D. Drazil and Tiles-暴力/拓扑排序
- Codeforces Round #292 (Div. 2)D. Drazil and Tiles
- Codeforces 516B Drazil and Tiles【类拓扑排序】好题!
- Codeforces 515D - Drazil and Tiles (拓扑排序)
- cf Round #292 (Div. 2)D. Drazil and Tiles 构造
- CF516B Drazil and Tiles(拓扑排序)
- Codeforces Round #292 (Div. 1)---A. Drazil and Factorial
- Codeforces Round #292 (Div. 1) C. Drazil and Park
- 【codeforces 516B】Drazil and Tiles
- #292 (div.2) D.Drazil and Tiles
- Codeforces Round #292 (Div. 2) -- B. Drazil and His Happy Friends
- 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
- android应用的界面编程
- SGU 101 Domino
- 1.HCNA-HNTD——企业网络架构介绍
- 安卓仿iphone-ibooks的反转视图特效
- 诗、词、歌、赋的区别
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓扑排序)
- TRIZ随记(2)
- UIReferenceLibraryViewController的使用(调用系统词典)
- Emacs markdown mode C-c C-c p预览中文乱码
- HDU 1596 find the safest road
- Codeforces Round #291 (Div. 2)C.Watto and Mechanism——字典树+dfs
- 一种基于穷举碰撞的RSA降维破解算法
- Android 自己试验Ip地址的时候,不要用localhost 要用ipconfig 进行查 IPV4
- POJ3905 Perfect Election【2-SAT】【Tarjan】