两次DFS,POJ(1481)

题目链接:http://poj.org/problem?id=1481

 两次DFS,这里的思路是,没找到*,就说明,有一个骰子,因此,每搜索到一个*,深搜4个方向,并且变为'.',要是搜到'X',就是骰子的点数++,而且将他的四周的'X'变为'.'

 

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>

using namespace std;

char a[50+5][50+5];
int count;

int i_cmp(void const *x,void const *y)
{
    return *(int*)x-*(int*)y;
}

void DFS_X(int x,int y)
{
    if(a[x][y]!='X')
        return ;
    else
        a[x][y]='.';
    DFS_X(x-1,y);    ///题目要求只需要考虑上下左右四个方向即可,而不是八个方向
    DFS_X(x,y-1);
    DFS_X(x,y+1);
    DFS_X(x+1,y);
}

void DFS(int x,int y)
{
    if(a[x][y]=='.')
        return ;
    if(a[x][y]=='X')
    {
        DFS_X(x,y);
        count++;
    }
    a[x][y]='.';
    DFS(x-1,y);
    DFS(x,y-1);
    DFS(x,y+1);
    DFS(x+1,y);
}

int main()
{
    int w,h,num=1,ct,dote[100];
    while(scanf("%d%d",&w,&h)!=EOF)
    {
        if(w==0&&h==0) break;
        memset(a,'.',sizeof(a));
        for(int i=1; i<=h; i++)
        {
            getchar();
            for(int j=1; j<=w; j++)
                scanf("%c",&a[i][j]);
        }
        ct=0;
        for(int i=1; i<=h; i++)
            for(int j=1; j<=w; j++)
                if(a[i][j]=='*')
                {
                    count=0;
                    DFS(i,j);
                    dote[ct++]=count;
                }
        printf("Throw %d\n",num++);
        qsort(dote,ct,sizeof(dote[0]),i_cmp);
        for(int i=0; i<ct; i++)
        {
            if(i)
                printf(" ");
            printf("%d",dote[i]);
        }
        printf("\n\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/TreeDream/p/5513344.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值