657 - The die is cast(dfs)
来源:互联网 发布:jsp登陆界面源码 编辑:程序博客网 时间:2024/06/05 18:30
题目:657 - The die is cast
题目大意:就是看图中的骰子点数。
解题思路:两次深度遍历,第一次先找骰子,第二次在找点数,本来是用一个数组来记录这个地方是否访问过,但是后面发现这样对于两次的dfs会出现问题,所以就将访问过的X改成*,访问过的*改成点,这样就可以达到二次遍历。先找不是 .(点) 的字符,然后在这里面找X的块数,这个就是点数。
再次明确了,做题要细心。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int N = 55;int w, h, t = 0;char G[N][N];int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};int answer[N * N], k;void dfs(int x, int y) {G[x][y] = '*';for(int i = 0; i < 4; i++) {int x1 = x + dir[i][0];int y1 = y + dir[i][1];if(x1 >= 0 && x1 < h && y1 >= 0 && y1 < w) {if(G[x1][y1] == 'X')dfs(x1, y1);}}}void handle(int x, int y) {if(G[x][y] == 'X' ) {dfs(x, y);answer[k]++;}G[x][y] = '.';for(int i = 0; i < 4; i++) {int x1 = x + dir[i][0];int y1 = y + dir[i][1];if(x1 >= 0 && x1 < h && y1 >= 0 && y1 < w) {if(G[x1][y1] != '.')handle(x1, y1);}}}int main() {while(scanf("%d%d%*c", &w, &h), w || h) {t++;memset(G, 0, sizeof(G));memset(answer, 0, sizeof(answer));int i, j;for( i = 0; i < h; i++)scanf("%s", G[i]);k = 0;for(i = 0; i < h; i++)for(j = 0; j < w; j++) {if(G[i][j] != '.') {handle(i, j);if(answer[k])k++;}}printf("Throw %d\n", t);if(k == 0) {printf("0\n\n");continue;}sort(answer, answer + k );for(i = 0; i < k ;i++) {if(i != k - 1)printf("%d ", answer[i]);else printf("%d\n\n", answer[i]);}}return 0;}
0 0
- 657 - The die is cast(dfs)
- The die is cast (DFS 双重)
- UVa 657 The die is cast(DFS)
- uva 657 The die is cast(DFS遍历图)
- UVa 657 The die is cast(DFS*2)
- UVA - 657 The die is cast(二重dfs)
- UVA - 657 - The die is cast (两次DFS!!)
- UVA 657 The die is cast (DFS*2)
- UVA 657 The die is cast ——bfs+dfs
- UVA 657-The die is cast(dfs*2)
- UVa-657 The die is cast(dfs嵌套)
- poj1481 The Die Is Cast(两个DFS)
- UVa657 The die is cast(DFS)
- DFS——The die is cast
- 657 - The die is cast(****)
- 657 - The die is cast
- 657 - The die is cast
- 657 - The die is cast
- android Camera拍照 及 MediaRecorder录像 预览图像差90度
- github总结
- MINA之心跳协议运用
- Hdu1053 step5.2.8(哈夫曼树)
- Android 混编 防止 反编译
- 657 - The die is cast(dfs)
- UVaOJ-674 Coin Change (完全背包)
- 去大公司实习还是去创业公司实习好?
- 寒假训练--训练赛2--冰冰的生日
- Android上用模板方法模式实现具有自动重用View功能的Adapter
- java中使用log4j 2
- Solr的HBase多条件查询
- windows下用Python连接mysql报错问题“MySQLdb in Python: “Can't connect to MySQL server on 'localhost'””
- 题目63:猴子下落