社区讨论

wa求助

P1443马的遍历参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mhjrewf8
此快照首次捕获于
2025/11/04 07:16
4 个月前
此快照最后确认于
2025/11/04 07:16
4 个月前
查看原帖
wa求助
CPP
#include<bits/stdc++.h>
using namespace std;
const int N=409;
int n,m,x,y,ne[8][2]={{3,2},{-3,2},{3,-2},{-3,-2},{2,3},{2,-3},{-2,3},{-2,-3}},a[N][N],vis[N][N];

struct node{
    int x,y,step; 
};
queue<node> q;
void bfs(int xi,int yi){
    q.push({xi, yi, 0});
    vis[xi][yi] = 1;
    a[xi][yi] = 0;
    while(!q.empty()){
        node n1=q.front();
        q.pop();
        for(int i=0;i<8;i++){
            int tx=n1.x+ne[i][0];
            int ty=n1.y+ne[i][1];
            if(tx>=1 && tx<=n && ty>=1 && ty<=m && !vis[tx][ty]){
                vis[tx][ty] = 1;
                a[tx][ty] = n1.step + 1;
                q.push({tx, ty, n1.step + 1});
            }
        }
    }
}

int main(){
    cin>>n>>m>>x>>y;
    bfs(x,y);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    
    return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...