BZOJ 3406 乳草的入侵

BFS。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 150
using namespace std;
queue <int> q;
int n,m,mx,my,map[maxn][maxn];
int dx[]={0,-1,-1,-1,0,1,1,1,0},dy[]={0,-1,0,1,1,1,0,-1,-1};
char s[maxn];
bool judge(int x,int y)
{
    if ((x>=1) && (x<=n) && (y>=1) && (y<=m))
        return true;
    return false;
}
int bfs()
{
    int mmx=0;
    q.push(mx);q.push(my);q.push(0);map[mx][my]=1;
    while (!q.empty())
    {
        int hx,hy,step;
        hx=q.front();q.pop();hy=q.front();q.pop();step=q.front();q.pop();
        for (int i=1;i<=8;i++)
        {
            int nowx=hx+dx[i],nowy=hy+dy[i];
            if ((judge(nowx,nowy)) && (!map[nowx][nowy]))
            {
                map[nowx][nowy]=1;
                mmx=max(mmx,step+1);
                q.push(nowx);q.push(nowy);q.push(step+1);
            }
        }
    }
    return mmx;
}
int main()
{
    scanf("%d%d%d%d",&m,&n,&mx,&my);
    swap(mx,my);mx=n-mx+1;
    for (int i=1;i<=n;i++)
    {
        scanf("%s",s);
        for (int j=0;j<m;j++)
            if (s[j]=='*') map[i][j+1]=1;
    }
    printf("%d
",bfs());
    return 0;
}
原文地址:https://www.cnblogs.com/ziliuziliu/p/5701023.html