社区讨论

P1443,0分

P1443马的遍历参与者 1已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@m5zaluk1
此快照首次捕获于
2025/01/16 20:14
去年
此快照最后确认于
2025/01/16 20:28
去年
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
struct P{
	int x,y;
}o,b;

queue<P> q;

const int dx[8] = {1,-1,1,-1,2,-2,2,-2};
const int dy[8] = {2,2,-2,-2,1,1,-1,-1};

int ans[410][410];
int main(){
	
	int m,n,x,y;
	cin>>n>>m>>x>>y;
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			ans[i][j] = -1;
		}
	}
	
	ans[x][y] = 0;
	o.x = x, o.y = y;
	q.push(o);
	
	while (!q.empty()) {
		o = q.front();
		q.pop();
		
		for(int t = 0 ; t < 8; t++){
			x = o.x + dy[t];
			y = o.y + dy[t];
			if ( x < 1 || x > n || y < 1 || y > m) continue;
			if ( ans[x][y] != -1) continue;
			ans[x][y] = ans[o.x][o.y] + 1;
			b.x = x; b.y = y;
			q.push(b);
		}
	}
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j ++){
			cout << ans[i][j] << (" \n"[j == m]);
		}
	}

	return 0;
}
//0分

回复

1 条回复,欢迎继续交流。

正在加载回复...