求大佬看看,悬赏关注,谢谢
查看原帖
求大佬看看,悬赏关注,谢谢
943490
husa_husa楼主2023/3/29 11:36

看了测试点1,和我的输出结果一样,但是就是不让过

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef pair<int,int> PII;
const int N = 500;
int dist[N][N];
int n,m,x,y;
int dx[] = {-2,-1,1,2,-2,1,-1,-2};
int dy[] = {1,2,2,1,-1,-2,-2,-1};
queue<PII> q;
void bfs(int x,int y){
    q.push({x,y});
    dist[x][y]=0;
    
    while(!q.empty()){
        auto t = q.front();
        q.pop();
        
        for(int i = 0;i < 8;++i){
            int a = t.first + dx[i];
            int b = t.second + dy[i];
            if(a<=0||a>n||b<=0||b>m)continue;
            if(dist[a][b]!=-1)continue;
            
            q.push({a,b});
            dist[a][b] = dist[t.first][t.second] + 1;
            
        }
        
    }
}
int main(){
    cin>>n>>m>>x>>y;
    memset(dist,-1,sizeof dist);
    bfs(x,y);
    for(int i = 1;i<=n;++i){
        for(int j = 1;j <= m;++j){
            printf("%d ",dist[i][j]);
        }
        cout<<endl;
    }
    return 0;
}
2023/3/29 11:36
加载中...