关于我不开O2 AC,开了以后RE

P1443 马的遍历

xuyichen123 @ 2024-12-07 20:27:42

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,tx,ty;
struct node{int x,y,step;};
bool vis[1010][1010];
int ans[1010][1010];
int dx[]={-2,-2,2,2,1,-1,1,-1},dy[]={-1,1,-1,1,2,-2,-2,2};
int bfs()
{
    queue<node> q;
    q.push({tx,ty,0});
    while(q.size())
    {
        int x=q.front().x,y=q.front().y,step=q.front().step;
        q.pop();
        for(int i=0;i<8;i++)
        {
            int xx=x+dx[i],yy=y+dy[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&ans[xx][yy]==-1)
            {
                q.push({xx,yy,step+1});
                ans[xx][yy]=step+1;
            }
        }
    }
}
signed main()
{
    cin>>n>>m>>tx>>ty;
    memset(ans,-1,sizeof ans);
    ans[tx][ty]=0;
    bfs();
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            printf("%-5d",ans[i][j]);
        printf("\n");
    }
    return 0;
}

开了O2RE,不开反而AC???

哪位dalao解释一下是什么原理qwq


by lfynbklsKNYYDS888666 @ 2024-12-07 20:34:23

w(゚Д゚)w


by cly312 @ 2024-12-07 20:37:46

bfs没return


by Oranges1234567 @ 2024-12-07 20:39:55

bfs()函数缺失了返回值,可以考虑改成void bfs(),或者加上return 0


by xuyichen123 @ 2024-12-07 21:51:32

谢谢各位dalao


|