首页 > 代码库 > hdu-1312 Red and Black

hdu-1312 Red and Black

http://acm.hdu.edu.cn/showproblem.php?pid=1312

题意:在图中给定一个人的起点,问他能达到的最多地方是多少,‘.‘表示能走,’#‘不能走。

思路:找出起点,直接扩展就好了。

#include<cstdio>
#include<cstring>
char map[21][21];
int dir[4][2]={-1,0,1,0,0,1,0,-1};
int n,m,count;

void dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]=='.')
        {
            map[xx][yy]='#';
            count++;
            dfs(xx,yy);
        }
    }
}

int main()
{
    int a,b;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(n==0&&m==0) break;
        for(int i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(int j=0;j<m;j++)
                if(map[i][j]=='@')
                {
                    count=1;
                    a=i;b=j;
                }
        }
        dfs(a,b);
        printf("%d\n",count);
    }
    return 0;
}


 

hdu-1312 Red and Black