HDU1241 DFS

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
    int x,y;
}node;
int maps[105][105];
int status[105][105];
node oil[10005];
int n,k,total;
int dir_v[8]={-1,-1,-1,0,0,1,1,1};
int dir_l[8]={-1,0,1,-1,1,-1,0,1};
void dfs(int i,int j)
{
    int h;
    int ti,tj;
    status[i][j]=1;
    for(h=0;h<8;h++)
    {
        ti= i+dir_v[h]; tj=j+dir_l[h];
        if(ti>=1&&ti<=n&&tj>=1&&tj<=k&&maps[ti][tj]&&!status[ti][tj])
            dfs(ti,tj);
    }
}
int main()
{
    int i,j,count,h;
    char ch;
    while(scanf("%d%d",&n,&k)&&(n||k))
    {
        scanf("\n");count=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=k;j++)
            {
                scanf("%c",&ch);
                if(ch=='*')maps[i][j]=0;
                else if(ch=='@')
                {
                    maps[i][j]=1;
                    oil[count].x=i;oil[count].y=j;
                    count++;
                }
            }
            if(i!=n)
                scanf("\n");
        }
        memset(status,0,sizeof(status));

        total=0;
        for(h=0;h<count;h++)
        {
            if(!status[oil[h].x][oil[h].y])
            {
                total +=1;
                dfs(oil[h].x,oil[h].y);
            }
        }
        printf("%d\n",total);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值