codeforces 515d Drazil and Tiles
来源:互联网 发布:隐马尔可夫算法 编辑:程序博客网 时间:2024/04/30 11:19
题意:
有一张图,让你往里面填"<>"或者"v^",问是否只有一种填法,如果只有一种填法,则输出具体方案;没有方案或者方案有2种及以上,则输出"Not unique"。
思路:
看了官方题解,自己也想了一下,感觉顶对的。
步骤:
1.首先把每个‘.‘看成是一个顶点,求出每个'.'与周围的’.‘的点连成图的度数。(如果有度数为0的点,则是无解"Not unique")
2.把度数为1的放进队列。
3.从队列中取出第一个顶点,把与其匹配的点标记掉。然后查看是否有没有被标记且度数减为1的点,有则放进队列。
4.继续操作,直到队列为空。若此时还有没有标记的点,则输出"Not unique"。否则,输出方案。
———————————————————————————————————————————————————
后来问了铭神要怎么才能区分没有方案和2种以上的方案。被告之,其实这货是个二分图。。。
在一个连通块内,剩下的点的度数都>=2且黑白点数相同(相邻的点互为黑白),则可以判断这个连通块一定是有解的,并且至少有两个解(可以查看官方题解)。
code:
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN = 2*1e3+5;int n, m, cnt;char ch[MAXN][MAXN];int degree[MAXN][MAXN];int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};char tag[4][5] = {"v^", "^v", "><", "<>"};queue <pair<int, int> > que;bool test(int x, int y) { int t1 ,t2; for(int i = 0;i < 4; i++) { t1 = x+dir[i][0], t2 = y+dir[i][1]; if(t1 >= 0 && t1 < n &&t2 >= 0 && t2 < m && ch[t1][t2] == '.') { degree[t1][t2]--; if(degree[t1][t2] == 0) return false; if(degree[t1][t2] == 1) que.push(make_pair(t1, t2)); } } return true;}bool solve() { int x, y; for(int i = 0; i < n; i++) { for(int j = 0;j < m; j++) { if(ch[i][j] != '.') continue; for(int t = 0;t < 4; t++) { x = i+dir[t][0], y = j+dir[t][1]; if(x >= 0 &&x < n && y >= 0 &&y < m && ch[x][y] == '.') degree[i][j]++; } //cout<<degree[i][j]<<endl; if(degree[i][j] == 0) return false; if(degree[i][j] == 1) que.push(make_pair(i, j)); } } // //cout<<"que size = "<<que.size()<<endl; pair <int, int> t; while(!que.empty()) { t = que.front(); que.pop(); for(int i = 0;i < 4; i++) { x = t.first + dir[i][0], y = t.second + dir[i][1]; if(x >= 0 &&x < n && y>=0&&y < m && ch[x][y] == '.') { ch[t.first][t.second] = tag[i][0]; ch[x][y] = tag[i][1]; cnt -= 2; if(!test(x, y)) return false; break; } } } if(cnt == 0) return true; return false;}void print() { for(int i = 0;i < n; i++) printf("%s\n", ch[i]);} int main() { scanf("%d%d", &n, &m); for(int i = 0;i < n; i++) { scanf("%s", ch[i]); for(int j = 0;j < m; j++) { if(ch[i][j] == '.') cnt++; } } // if(solve()) print(); else puts("Not unique"); return 0;}
0 0
- codeforces 515D Drazil and Tiles
- codeforces 515d Drazil and Tiles
- 【codeforces】515-D Drazil and Tiles
- codeforces 515D Drazil and Tiles
- Drazil and Tiles - CODEFORCES 515D 贪心
- Drazil and Tiles(CodeForces 515D)
- Codeforces 515D - Drazil and Tiles (拓扑排序)
- codeforces 515D Drazil and Tiles(构造)
- CODEFORCES 515D Drazil and Tiles <路径dfs + 跳格>
- Drazil and Tiles - CodeForces 513 D 搜索
- 【CF 515D】 Drazil and Tiles
- Codeforces Round #292 (Div. 2)D. Drazil and Tiles
- codeforces#292-D. Drazil and Tiles-暴力/拓扑排序
- D. Drazil and Tiles (CF 515D bfs搜索)
- #292 (div.2) D.Drazil and Tiles
- 【codeforces 516B】Drazil and Tiles
- Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)
- Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)
- 一道不怎么理解的题
- codeforces 515c Drazil and Factorial 分类讨论
- uva 10803(floyd变式)
- Failed to start LSB: Bring up/down networking
- 初中生可能学好编程么?
- codeforces 515d Drazil and Tiles
- Subsets
- Java中的TreeMap、Comparable、Comparator
- 历届试题 兰顿蚂蚁
- codevs1082 线段树练习3
- 《数据结构编程实验》 1.5.4A Contesting Decision
- oracle中解决被锁定的scott用户的方法。
- 【Unity】查看、使用Untiy内部库和方法
- Ajax使用范例