社区讨论

求助!全WA

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

讨论操作

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

当前回复
9 条
当前快照
1 份
快照标识符
@lrrdw42h
此快照首次捕获于
2024/01/24 14:08
2 年前
此快照最后确认于
2024/01/24 16:18
2 年前
查看原帖
CPP

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef pair<int,int> PII;

#define x first
#define y second

const int N=410;
int g[N][N];
PII q[N*N];
int n,m,x,y;
int cnt=0;

void bfs(int x,int y){
    int hh=0,tt=0;
    q[0]={x,y};
    g[x][y]=0;
    
    int dx[8]={1,2,2,1,-1,-2,-2,-1};
    int dy[8]={2,1,-1,-2,-2,-1,1,2};
    while(hh<=tt){
        auto t=q[hh++];
        for(int i=0;i<8;i++){
            int sx=t.x+dx[i];
            int sy=t.y+dy[i];
            if(sx<0||sx>n-1||sy<0||sy>m-1) continue;
            if(g[sx][sy]!=-1) continue;
            g[sx][sy]=g[t.x][t.y]+1;
            q[++tt]={sx,sy};
            
        }
    }
}

int main(){
    memset(g,-1,sizeof(g));
    cin>>n>>m>>x>>y;
    bfs(x,y);

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<g[i][j]<<" ";
        }
        puts("");
    }
    
    return 0;
}

回复

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

正在加载回复...