还是用dfs打bfs
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[407][407];
int n,m,x,y;
void dfs(int x,int y,int k){
if(a[x][y]==-1){
a[x][y]=k;
}
else if(a[x][y]>k){
a[x][y]=k;
}
if(x-1>=1&&y+2<=m) dfs(x-1,y+2,k+1);
if(x+1<=n&&y+2<=m) dfs(x+1,y+2,k+1);
if(x-1>=1&&y-2>=1) dfs(x-1,y-2,k+1);
if(x+1<=n&&y-2>=1) dfs(x+1,y-2,k+1);
if(x+2<=n&&y+1<=m) dfs(x+2,y+1,k+1);
if(x+2<=n&&y-1>=1) dfs(x+2,y-1,k+1);
if(x-2>=1&&y-1>=1) dfs(x-2,y-1,k+1);
if(x-2>=1&&y+1<=m) dfs(x-2,y+1,k+1);
}
signed main(){
cin.tie(0);
cout.tie(0);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[i][j]=-1;
}
}
cin>>n>>m>>x>>y;
a[x][y]=0;
dfs(x,y,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<a[i][j]<<' ';
}
putchar('\n');
}
}