BFS 求助(悬赏关注)
查看原帖
BFS 求助(悬赏关注)
1069781
Lzj_qwq楼主2024/3/9 13:21
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int walk[8][2]={
	{2,1},{-2,1},{2,-1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}
};
struct coord{
	int x,y;
};
queue<coord> q;
int book[402][402];
int main(){
	int n,m,sx,sy;
	scanf("%d%d%d%d",&n,&m,&sx,&sy);
	q.push((coord){sx,sy});
	memset(book,-1,sizeof(book));
	book[sx][sy]=0;
	while(q.empty()){
		coord e=q.front(); q.pop();
		for(int i=0;i<8;++i){
			int x=e.x+walk[i][0],y=e.y+walk[i][1];
			if(x<1 || x>n || y<1 || y>m || book[x][y]!=-1)
				continue;
			book[x][y]=book[e.x][e.y]+1;
			q.push((coord){x,y});
		}
	}
	for(int i=1;i<=n;++i,puts(" "))
		for(int j=1;j<=m;++j)
			printf("%-5d",book[i][j]);
	return 0;
}
2024/3/9 13:21
加载中...