HDU 1241

#include<stdio.h>
char str[102][102];
int d[8][2]={1,1, 1,-1, -1,1, -1,-1, 0,1, 0,-1, 1,0, -1,0};
int m,n;
void dfs(int x,int y)
{
    str[x][y] = '*';
    int i,nx,ny;
    for(i = 0;i <= 7;i ++)
    {
        nx = x+d[i][0];
        ny = y+d[i][1];
        if(nx > -1 && nx < m && ny > -1 && ny < n && str[nx][ny] == '@')
              dfs(nx,ny);
    }
    return ;
}

int main()
{
    int t,k,cnt;
    while(~scanf("%d%d",&m,&n)&&m)
    {
        cnt = 0;
        for(t = 0;t < m;t ++)
            scanf("%s",str[t]);
        for(t = 0;t < m;t ++)
        {
            for(k = 0;k < n;k ++)
            {
                if(str[t][k] == '@')
                {
                    dfs(t,k);
                    cnt++;
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}


posted on 2014-04-30 18:35  wangzhili  阅读(64)  评论(0编辑  收藏  举报