hdu 杭电 1312 Red and Black 果枫

题意:W H分别代表图的列与行,图中有'.' '#' '@' 从@的位置出发最多能搜到多少个‘.’,‘#’为障碍物。

解法:广搜

ac代码:

View Code
#include<iostream>
#include<queue>
using namespace std;

const int M=20+3;
char map[M][M];//地图
bool use[M][M];//用作标记
int v[4][2]={-1,0,0,-1,1,0,0,1};//方向向量:左,上,右,下

struct que
{
    int i,j;
};

int main()
{
    int m,n;
    int i,j;
    int c,d;
    int count;
    queue<que>q;
    que temp;
    while(scanf("%d%d",&m,&n)!=EOF&&(m||n))
    {
        getchar();
        count=1;
        memset(use,0,sizeof(use));

        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='.')
                    use[i][j]=1; //当use[][]==0,说明此点为障碍物,不能走
                else
                    if(map[i][j]=='@')
                    {
                        c=i;
                        d=j;
                    }
            }
            getchar();
        }

        temp.i=c;temp.j=d;
        q.push(temp);

        while(!q.empty())
        {
            use[q.front().i][q.front().j]=0;

            for(i=0;i<4;i++)//一个点能生出四个点
            {
                c=q.front().i+v[i][0];
                d=q.front().j+v[i][1];

                if((c>=1&&c<=n)&&(d>=1&&d<=m)&&use[c][d])//判断是否出界及是否为障碍物
                {
                    temp.i=c;temp.j=d;
                    q.push(temp);
                    use[c][d]=0;
                    count++;
                } 
            }

            q.pop();
        }

        printf("%d\n",count);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zgfailmr/p/2665886.html