longjh100810 @ 2024-08-22 10:11:01
#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y;
long long step;
}q[160003];
int a[405][405];
int n,m,xx,yy;//题目中的“x”“y”我用的xx,yy
int nx[8]={2,1,-1,-2,-2,-1,1,2};
int ny[8]={1,2,2,1,-1,-2,-2,-1};
void bfs(int x,int y,int h,int l){
long long head=1,tail=1;
q[head].step=1;
a[x][y]=0;
q[tail].x=x;
q[tail].y=y;
tail++;
while(head<tail){
for(int i=0;i<8;i++){
int dx=q[head].x+nx[i],dy=q[head].y+ny[i];
if(dx>=1&&dx<=n && dy>=1&&dy<=m && a[dx][dy]==1){
a[x][y]=0;
q[tail].x=dx;
q[tail].y=dy;
q[tail].step=q[head].step+1;
if(dx==h&&dy==l){
printf("%-5d",q[tail].step-1);
return ;
}
tail++;
}
}
head++;
}
if(xx==h&&yy==l){
cout<<"0 ";
return ;
}
cout<<"-1 ";
return ;
}
int main() {
cin>>n>>m>>xx>>yy;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
bfs(xx,yy,i,j);
for(int i1=1;i1<=n;i1++) for(int j1=1;j1<=m;j1++) a[i1][j1]=1;
}
cout<<endl;
}
return 0;
}