WA on #9 #10
查看原帖
WA on #9 #10
1015482
huzixiao楼主2025/2/3 22:14
#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;
}
2025/2/3 22:14
加载中...