10分求助
查看原帖
10分求助
1078964
equipy_rrt楼主2024/10/16 16:53
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int dx[]={-1,1,2,-2,1,-2,2,-1};
const int dy[]={2,2,1,1,-2,-1,-1,-2};
int n,m,x,y,step=1,a[403][403];
struct coord{
	int h,z;	
	bool f;
};
queue<coord> q;

int main(){
	memset(a,-1,sizeof(a));
	scanf("%d%d%d%d",&n,&m,&x,&y);
	a[x][y]=0;
	coord s;
	s.h=x,s.z=y,s.f=0;
	q.push(s);
	while(!q.empty()){
		for(int i=0;i<8;i++)
			if(!q.front().f && q.front().h+dx[i]>=1 && q.front().h+dx[i]<=n && q.front().z+dy[i]>=1 && q.front().z+dy[i]<=m){
				coord k;
				k.h=q.front().h+dx[i];
				k.z=q.front().z+dy[i];
				a[k.h][k.z]=step;
				q.front().f=k.f=1;
				q.push(k);
			}
		q.pop();
		step++;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) printf("%d    ",a[i][j]);
		printf("\n");
	}
	return 0;
}
2024/10/16 16:53
加载中...