UVA - 657 The die is cast

题目大意:求骰子上的点数并按升序输出。. 为背景,* 为骰子, 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 输出不太一样……迷

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值