P2960
贴上82分的代码,巨佬帮忙看看改改
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int m[N][N];
bool vis[N][N];
int dir[9][2]={{0,0},{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
int x,y,mx,my;
struct nd{
int x, y;
nd(int x_, int y_){
x=x_, y=y_;
}
};
queue<nd> q,q1;
int main(){
cin >> x >> y >> mx >> my;
for(int i=1;i<=x;i++){
for(int j=1;j<=y;j++){
char c;
cin >> c;
if(c=='.')
m[i][j]==0;
else
m[i][j]==1;
//m[i][j]=(c=='.'?0:1);
}
}
vis[mx][my]=1;
q.push(nd(mx,my));
int cnt=0;
while(true){
bool flag=false;
while(!q.empty()){
nd a=q.front();
q.pop();
for(int i=1;i<=8;i++){
int nx=a.x+dir[i][0],ny=a.y+dir[i][1];
if(nx<1||nx>x||ny<1||ny>y) continue;
if(vis[nx][ny]==0&&m[nx][ny]==0){
flag=true;
vis[nx][ny]=1;
q1.push(nd(nx,ny));
}
}
}
if(flag==true) cnt++;
if(q1.empty()) break;
swap(q,q1);
}
cout << cnt << endl;
return 0;
}