HDU 1312 Red and Black

水题~

const int N=25;
char g[N][N];
bool vis[N][N];
int n,m;
PII st;

bool check(int x,int y)
{
    return x>=0 && x<n && y>=0 && y<m;
}

int bfs()
{
    queue<PII> q;
    q.push({st.fi,st.se});
    vis[st.fi][st.se]=true;

    int res=1;
    while(q.size())
    {
        PII t=q.front();
        q.pop();

        for(int i=0;i<4;i++)
        {
            int a=t.fi+dx[i],b=t.se+dy[i];
            if(!check(a,b) || g[a][b] == '#') continue;
            if(!vis[a][b])
            {
                res++;
                vis[a][b]=true;
                q.push({a,b});
            }
        }
    }
    return res;
}

int main()
{
    while(cin>>m>>n)
    {
        if(!n && !m) break;

        memset(vis,0,sizeof vis);

        for(int i=0;i<n;i++) scanf("%s",&g[i]);

        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(g[i][j] == '@')
                    st={i,j};

        int t=bfs();
        cout<<t<<endl;
    }
    //system("pause");
}
原文地址:https://www.cnblogs.com/fxh0707/p/14160956.html