求修改 开o2 TLE两个,万分感谢!!!
#include <bits/stdc++.h>
using namespace std;
int vist[405][405],n,m,x,y;
int xk[8]={1,2,2,1,-1,-2,-2,-1};
int yk[8]={2,1,-1,-2,-2,-1,1,2};
struct lou{
int x,y;
int bu;
};
queue<lou>dui;
int bfs(int a,int b){
while(!dui.empty()){
dui.pop();
}
memset(vist,0,sizeof(vist));
struct lou cur,now;
cur.x=x;cur.y=y;cur.bu=0;
dui.push(cur);
vist[x][y]=1;
while(dui.empty()==false){
now=dui.front();
dui.pop();
if(now.x==a&&now.y==b){
return now.bu;
}
int xx,yy;
for(int p=0;p<8;p++){
xx=now.x+xk[p];
yy=now.y+yk[p];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vist[xx][yy]==0){
//cout<<xx<<"----"<<yy<<" ";
vist[xx][yy]=1;
cur.x=xx;cur.y=yy;
cur.bu=now.bu+1;
dui.push(cur);
}
}
}
return -1;
}
int main(){
cin>>n>>m>>x>>y;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
printf("%-5d",bfs(i,j));
}
cout<<endl;
}
return 0;
}