MLE玄关
查看原帖
MLE玄关
1063799
chenshenqi楼主2024/6/15 19:59
#include <bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for (int i=(a); i<(b); i++)
#define __for(i,a,b) for (int i=(a); i<=(b); i++)
#define endl "\n";
struct Pos{
    int x;
    int y;
    int cost;
    Pos(int ax=0,int ay=0,int acost=0){
        x=ax,y=ay,cost=acost;
    }
};
queue<Pos> q;
bool vis[405][405];
int step[405][405];
int n,m,x,y;
const int horse_x[]={-1,-2,-2,-1,1,2,2,1};
const int horse_y[]={2,1,-1,-2,2,1,-1,-2};
void bfs(){
    q.push(Pos(x,y,0));
    step[x][y]=0;
    vis[x][y]=1;
    while (!q.empty()){
        Pos now=q.front();
        q.pop();
        int x=now.x,y=now.y,cost=now.cost;
        step[x][y]=cost;
        vis[x][y]=1;
        _for(i,0,8){
            int nx=x+horse_x[i],ny=y+horse_y[i];
            if (nx<1||ny<1||nx>n||ny>m||vis[nx][ny]) continue;
            q.push(Pos(nx,ny,cost+1));
        }
    }
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	memset(step,-1,sizeof(step));
    cin >> n >> m >> x >> y;
    bfs();
    __for(i,1,n){
        __for(j,1,m){
            printf("%-5d",step[i][j]);
        }
        printf("\n");
    }
	return 0;
}

AC*3

2024/6/15 19:59
加载中...