社区讨论

0分求助qwq

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

讨论操作

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

当前回复
3 条
当前快照
1 份
快照标识符
@lo2iugnz
此快照首次捕获于
2023/10/23 14:33
2 年前
此快照最后确认于
2023/10/23 14:33
2 年前
查看原帖
代码如下:
CPP
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int>> q;
int ans[310][310];
int dis[8][2] = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}};
int main() {
	int n, m, x, y;
	scanf("%d%d%d%d", &n, &m, &x, &y);
	memset(ans, -1, sizeof(ans));
	ans[x][y] = 0;
	q.push(make_pair(x, y));
	while (!q.empty()) {
		pair<int, int> cur = q.front();
		q.pop();
		for (int k = 0; k < 8; k++) {
			int newx = cur.first + dis[k][0], newy = cur.second + dis[k][1];
			if (newx < 1 || newx > n || newy < 1 || newy > m || ans[newx][newy] != -1) continue;
			ans[newx][newy] = ans[cur.first][cur.second] + 1;
			q.push(make_pair(newx, newy));
		}
	}
	for (int i = 0; i < n; i++, puts(""))
		for (int j = 0; j < m; j++)
			printf("%5d", ans[i][j]);
	return 0;
}

回复

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

正在加载回复...