bfs
统计其他点到源点距离
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int N=510;
int n,m,ex,ey;
char a[N][N];
int d[N][N];
const int dx[8]={0,0,1,1,1,-1,-1,-1};
const int dy[8]={1,-1,1,0,-1,1,0,-1};
int bfs()
{
queue<PII> q;
q.push({ex,ey});
d[ex][ey]=0;
int res=-0x3f3f3f3f;
while(!q.empty())
{
auto t=q.front(); q.pop();
for(int i=0;i<8;i++)
{
int x=dx[i]+t.first,y=dy[i]+t.second;
if(x<=0||x>m||y<=0||y>n||a[x][y]=='*'||d[x][y]!=-1) continue;
d[x][y]=d[t.first][t.second]+1;
res=max(res,d[x][y]);
q.push({x,y});
}
}
return res;
}
int main()
{
memset(d,-1,sizeof d);
int x,y;
cin>>n>>m>>x>>y;
ex=m-y+1,ey=x;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
cout<<bfs()<<endl;
return 0;
}