一个测试点RE,手写队列BFS

P1443 马的遍历

OIYu @ 2024-08-15 17:17:20

#include<bits/stdc++.h>
using namespace std;
int n,m,sx,sy;
int tx[16025],ty[16025],head,tail;
int a[]={1,1,-1,-1,2,2,-2,-2};
int b[]={2,-2,2,-2,1,-1,1,-1};
int f[405][405];
bool judge(int x,int y){
    if(x<1||x>n||y<1||y>m||f[x][y]!=-1)return false;
    return true;
}
int main(){
    cin>>n>>m>>sx>>sy;
    memset(f,-1,sizeof(f));
    tx[tail]=sx;
    ty[tail]=sy;
    f[sx][sy]=0;
    tail++;
    while(head<tail){
        int x=tx[head];
        int y=ty[head];
        int bu=f[x][y];
        head++;

        for(int i=0;i<8;i++){
            int xx=x+a[i];
            int yy=y+b[i];
            if(judge(xx,yy)==false)continue;
            tx[tail]=xx;
            ty[tail]=yy;
            f[xx][yy]=bu+1;
            tail++;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<f[i][j]<<" ";
        }
        cout<<"\n";
    }
    return 0;
}

by zhanglinuo @ 2024-08-17 10:33:20

tx和ty数组多加个零就行了


by yyz0526 @ 2024-08-19 13:21:39

@GrammarYu <<400*400=16000>>

想笑


by OIYu @ 2024-08-19 19:28:51

@zhanglinuo 谢谢,已关


|