TLE&0pts,玄关
  • 板块P1443 马的遍历
  • 楼主yu_rui
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/10/2 09:31
  • 上次更新2024/10/2 09:33:36
查看原帖
TLE&0pts,玄关
1053722
yu_rui楼主2024/10/2 09:31
#include<bits/stdc++.h>
using namespace std;
int x,y,n,m;
int a[500][500];
bool vis[500][500];
int dj[500][500];
struct node{
    int xx,yy;
};
queue<node> q;
int xxx[8]={-2,-1,1,2,2,1,-1,-2};
int yyy[8]={-1,-2,-2,-1,1,2,2,1};
bool check(int a,int b){
    return a>=1&&a<=n&&b>=1&&b<=m&&vis[a][b]==0;
}
void bfs(){
    q.push(node{x,y});
    vis[x][y]=1;
    dj[x][y]=0;
    int a,b;
    while(!q.empty()){
        for(int i=0;i<8;++i){
            a=q.front().xx;
            b=q.front().yy;
            a+=xxx[i];
            b+=yyy[i];
            if(check(a,b)){
                dj[a][b]=dj[q.front().xx][q.front().yy]+1;
                vis[a][b]=1;
            }
        }
    }
}
int main(){
    cin>>n>>m>>x>>y;
    memset(dj,-1,sizeof(dj));
    bfs();
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j){
            if(dj[i][j]<0) cout<<-1;
            else cout<<setw(5)<<dj[i][j];
        }
        cout<<"\n";
    }
    return 0;
}

0

2024/10/2 09:31
加载中...