SOS!玄关求助
查看原帖
SOS!玄关求助
1097079
_Fu_Fu_楼主2025/1/13 15:06
#include <bits/stdc++.h>
using namespace std;
const int N = 505;

struct coord {
	int x, y;
};

int a[N][N], n, m, sx, sy;
int walk[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {-1, 2}, {-1, -2}, {1, -2}};
queue<coord>Q;

void bfs() {
	while (!Q.empty()) {
		coord u = Q.front();
		Q.pop();
		int ux = u.x, uy = u.y;
		for (int i = 1; i <= 8; i++) {
			int x = ux + walk[i][0], y = uy + walk[i][1];
			int d = a[ux][uy];
			if (x < 1 || x > n || y < 1 || y > m || a[x][y != -1]) {
				continue;
			}
			a[x][y] = d + 1;
			coord tmp = {x, y};
			Q.push(tmp);
		}
	}

}

int main() {
	memset(a, -1, sizeof(a));
	cin >> n >> m >> sx >> sy;
	coord tmp =  {sx, sy};
	Q.push(tmp);
	a[sx][sy] = 0;
	bfs();
	for (int i = 1; i <= n; i++, puts(" ")) {
		for (int j = 1; j <= m; j++) {
			cout << setw(5) << a[i][j];
		}
	}
	return 0;
}
2025/1/13 15:06
加载中...