QWQ 20分蒟蒻求助
查看原帖
QWQ 20分蒟蒻求助
544452
YANGKAIHAN1楼主2021/11/14 07:56
#include<cstring>
#include<cstdio>
#include<queue>
#define N 404
using namespace std;
struct Coordinate{
	int x,y,cnt;
};
queue<Coordinate>crdt;
int vis[N][N];
int fx[10][2]={{0,0},{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
int sx,sy,m,n;
void bfs(int x,int y){
	crdt.push((Coordinate){x,y,0});
	while(!crdt.empty()){
		Coordinate now=crdt.front();
		int cnt=now.cnt;
		x=now.x;
		y=now.y;
		for(int i=0;i<8;i++){
			int xx=x+fx[i][0];
			int yy=y+fx[i][1];
			if(xx>=0 and yy>=0 and xx<m and yy<n and vis[xx][yy]<0){
				vis[xx][yy]=cnt+1;
				crdt.push((Coordinate){xx,yy,1+cnt});
			}
		}
		crdt.pop();
	}
}
int main(){
	memset(vis,-1,sizeof(vis));
	scanf("%d %d %d %d",&m,&n,&sx,&sy);
	sx--,sy--;
	vis[sx][sy]=0;
	bfs(sx,sy);
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			printf("%-4d ",vis[i][j]);
		}
		printf("\n");
	}
	return 0;
}
2021/11/14 07:56
加载中...