BFS60pts, 求调!!!
查看原帖
BFS60pts, 求调!!!
565707
mediocre_楼主2023/4/11 19:23
#include<bits/stdc++.h>
using namespace std;
const int N = 20 + 9;
const int fx[] = {1, 1, 2, 2, 2, 2, -1, -1, -2, -2, -2, -2};
const int fy[] = {-2, 2, -2, -1, 1, 2, -2, 2, -1, 1, -2, 2};
int x, y;
int vis[N][N];
bool use[N][N];
struct Node {
	int x, y;
};
queue <Node>q;
bool inmap(int nx, int ny) {
	return nx >= 1 && nx <= 20 && ny >= 1 && ny <= 20;
}
int main() {
	for (int i = 1; i <= 2; ++i) {
	    memset(vis, 0, sizeof vis);
	    memset(use, 0, sizeof use);
		scanf("%d%d", &x, &y);
		q.push(Node{x, y});
		use[x][y] = true;
		while (!q.empty()) {
			Node u = q.front();
			q.pop();
			if (u.x == 1 && u.y == 1) {
				printf("%d\n", vis[1][1]);
				break;
			}
			for (int d = 0; d < 12; ++d) {
				int nx = u.x + fx[d];
				int ny = u.y + fy[d];
				if (inmap(nx, ny) && !use[nx][ny]) {
					vis[nx][ny] = vis[u.x][u.y] + 1;
					use[nx][ny] = true;
					q.push(Node{nx, ny});
				}
			}
		}
		/*for (int j = 1; j <= 20; ++j) {
			for (int k = 1; k <= 20; ++k)
			    printf("%d ", vis[j][k]);
			printf("\n");
		}*/
	}
	return 0;
}
2023/4/11 19:23
加载中...