littlejohn @ 2024-08-10 22:26:53
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int table[401][401],n,m,x,y,v,xna,yna;
int stepx[]={1, 2 ,2,1,-1,-2,-2,-1},
stepy[]={-2,-1,1,2,2 ,1 ,-1,-2};
struct Pos{
int x,y,v;
};
queue<Pos> q;
Pos t,pos;
int main(){
memset(table,-1,sizeof(table));
cin>>n>>m>>x>>y;
table[x][y]=0;
pos.x = x;
pos.y = y;
pos.v = 0;
q.push(pos);
while(q.empty()==0){
pos = q.front();
q.pop();
if(table[pos.x][pos.y]==-1)table[pos.x][pos.y] = pos.v;
for(int i=0;i<=7;i++){
xna = pos.x+stepx[i];
yna = pos.y+stepy[i];
if((1<=xna && xna<=n)&&(yna>=1 &&yna<=m)){
if(table[xna][yna]==-1){
t.x = xna;
t.y = yna;
t.v = pos.v + 1;
q.push(t);
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<table[i][j]<<' ';
}
cout<<endl;
}
return 0;
}
最后就这样了 问题在哪里 求指导
by littlejohn @ 2024-08-11 19:52:49
wow,栓Q