fuck__luogu @ 2023-08-20 11:59:46
#include<bits/stdc++.h>
using namespace std;
int n,m,a[70][70],vis[70][70],mx,my,sx,sy,fx,fy;
int dx[8],dy[8];
struct node{
int x,y,step;
};
void bfs(int x,int y){
queue<node> q;
q.push({x,y,0});
while(!q.empty()){
node q1 = q.front();
q.pop();
for(int i = 0; i < 8; i++){
int xx = q1.x+dx[i],yy = q1.y+dy[i];
if(xx>0&&xx<=n&&yy>0&&yy<=m&&vis[xx][yy]>=q1.step+1&&a[xx][yy]!=0&&(a[xx][yy]==4||a[xx][yy]==1)){
vis[xx][yy] = q1.step+1;
if(a[xx][yy]==4) return;
q.push({xx,yy,q1.step+1});
}
}
}
}
int main(){
scanf("%d%d%d%d",&n,&m,&mx,&my);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
vis[i][j] = INT_MAX;
scanf("%d",&a[i][j]);
if(a[i][j] == 3) sx=i,sy=j,vis[i][j] = 0;
if(a[i][j] == 4) fx=i,fy=j;
}
}
dx[0] = mx,dx[1] = mx,dx[2] = -mx,dx[3] = -mx,dx[4] = my,dx[5] = -my,dx[6] = -my,dx[7] = my;
dy[0] = my,dy[1] = -my,dy[2] = -my,dy[3] = my,dy[4] = mx,dy[5] = mx,dy[6] = -mx,dy[7] = -mx;
bfs(sx,sy);
printf("%d",vis[fx][fy]);
return 0;
}