why_wpc @ 2024-10-19 11:16:19
#include<bits/stdc++.h>
using namespace std;
int mx[8]={2,-2,2,-2,-1,1,-1,1},my[8]={1,1,-1,-1,2,2,-2,-2},x,y,m,n,a[514][514],p[114514][2],ff,e=2;
int main(){
cin>>n>>m>>x>>y;
memset(a,-1,sizeof(a));
p[1][0]=x,p[1][1]=y;
a[x][y]=0;
while(ff++<e-1){
for(int i=0;i<8;i++){
int xx=p[ff][0]+mx[i],yy=p[ff][1]+my[i];
if(xx>0&&xx<=n&&yy>0&&y<=m&&a[xx][yy]==-1){
p[e][0]=xx,p[e++][1]=yy,a[xx][yy]=a[p[ff][0]][p[ff][1]]+1;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}