BZOJ 3406 乳草的入侵

时间:2023-03-10 01:57:35
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[]={,-,-,-,,,,,},dy[]={,-,,,,,,-,-};
char s[maxn];
bool judge(int x,int y)
{
if ((x>=) && (x<=n) && (y>=) && (y<=m))
return true;
return false;
}
int bfs()
{
int mmx=;
q.push(mx);q.push(my);q.push();map[mx][my]=;
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=;i<=;i++)
{
int nowx=hx+dx[i],nowy=hy+dy[i];
if ((judge(nowx,nowy)) && (!map[nowx][nowy]))
{
map[nowx][nowy]=;
mmx=max(mmx,step+);
q.push(nowx);q.push(nowy);q.push(step+);
}
}
}
return mmx;
}
int main()
{
scanf("%d%d%d%d",&m,&n,&mx,&my);
swap(mx,my);mx=n-mx+;
for (int i=;i<=n;i++)
{
scanf("%s",s);
for (int j=;j<m;j++)
if (s[j]=='*') map[i][j+]=;
}
printf("%d\n",bfs());
return ;
}