广度优先搜索之马的遍历,为什么通过不了?

img

#include<bits/stdc++.h>
using namespace std;
struct point{
    int x,y;
};
queue<point> q;
int ans[410][410];
int walk[8][2]={{2,1},{1,2},{2,-1},{1,-2},
{-2,1},{-1,2},{-2,-1},{-1,-2}};
int main(){
    int n,m,x,y;
    cin>>n>>m>>x>>y;
    memset(ans,-1,sizeof(ans));//-1表示未访问 
    point start={x,y};
    q.push(start);//使起点入队列 
    ans[x][y]=0;
    while(!q.empty()){
        point head=q.front();//拿出队首以扩展 
        int hx=head.x,hy=head.y;
        q.pop();
        for(int i=0;i<8;i++){
            int xx=hx+walk[i][0],yy=hy+walk[i][1];
            int d=ans[hx][hy];
            if(xx<1||xx>n||yy<1||yy>m||ans[xx][yy]!=-1){
                continue;//无需入队列 
            } 
            ans[xx][yy]=d+1;//记录答案,第几步 
            point next={xx,yy};
            q.push(next);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<ans[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}