您的位置:首页 > 其它

UVA - 657 The die is cast

2016-08-10 21:50 357 查看
题目大意:求骰子上的点数并按升序输出。. 为背景,* 为骰子, X 为点数,有公共边相邻点数为 1,即上下左右才算相邻。

解题思路:类似上一题油田的,多了一层遍历。查找骰子即 *,然后在骰子上查找点数即 X,双重 dfs。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char map[100][100];
int cn, cnt, tot, w, h;
int num[1000];
void dfs2(int r, int c, int cn) {
if (r < 0 || r >= h || c < 0 || c >= w) return;
if (map[r][c] != 'X') return;
map[r][c] = '*';

dfs2(r-1, c, cn);
dfs2(r, c-1, cn);
dfs2(r, c+1, cn);
dfs2(r+1, c, cn);

}
void dfs1(int r, int c) {
if (r < 0 || r >= h || c < 0 || c >= w || map[r][c] == '.') return;
if (map[r][c] == 'X') dfs2(r, c, ++num[tot]);
map[r][c] = '.';

dfs1(r-1, c);
dfs1(r, c-1);
dfs1(r, c+1);
dfs1(r+1, c);
}
int main() {
while (scanf("%d%d", &w, &h) != EOF && w && h) {
printf("Throw %d\n", ++cnt);
memset(num, 0, sizeof(num));
tot = 0;
for (int i = 0; i < h; i++)
scanf("%s", map[i]);

for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (map[i][j] == '*') {
dfs1(i, j);
tot++;
}
sort(num, num+tot);
for (int i = 0; i < tot-1; i++)
printf("%d ", num[i]);
printf("%d\n\n",num[tot-1]);
}
}


没想到这题能过,觉得写的时候挺乱的- -

debug 里面的样例输入和搜到的几个 AC 输出不太一样……迷
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 遍历 dfs