社区讨论

WA on #9 #10

P1747好奇怪的游戏参与者 2已保存回复 2

讨论操作

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

当前回复
2 条
当前快照
1 份
快照标识符
@m6p4uj6x
此快照首次捕获于
2025/02/03 22:15
去年
此快照最后确认于
2025/11/04 10:02
4 个月前
查看原帖
CPP
#include<iostream>
#include<queue>
#include<cstring>
#define in(x, y) x >= 0 && y >= 0 && x <= 20 && y <= 20
using namespace std;
struct node {
	int x, y;
};
int dis[25][25];
int bfs(int x, int y) {
	memset(dis, -1, sizeof dis);
	queue<node> q;
	q.push({x, y});
	dis[x][y] = 0;
	while (!q.empty()) {
		node now = q.front();
		q.pop();
		if (now.x == 1 && now.y == 1) {
			return dis[now.x][now.y];
		}
		int nextx[] = {-2, -2, -1, -1, +1, +1, +2, +2, -2, -2, +2, +2};
		int nexty[] = {-1, +1, -2, +2, -2, +2, -1, +1, -2, +2, -2, +2};
		for (int i = 0; i < 12; i++) {
			int tx = now.x + nextx[i];
			int ty = now.y + nexty[i];
			if (in(tx, ty) && dis[tx][ty] == -1) {
				dis[tx][ty] = dis[now.x][now.y] + 1;
				q.push({tx, ty});
			}
		}
	}
}
int main() {
	int x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	cout << bfs(x1, y1) << endl << bfs(x2, y2);
	return 0;
}

回复

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

正在加载回复...