WA求调
  • 板块P1443 马的遍历
  • 楼主cxlqwq
  • 当前回复9
  • 已保存回复9
  • 发布时间2024/7/28 10:29
  • 上次更新2024/7/28 11:55:45
查看原帖
WA求调
1319065
cxlqwq楼主2024/7/28 10:29
#include <bits/stdc++.h>
using namespace std;

int n, m;
bool b[401][401];
int ans[401][401];
int opt[8][2] = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}};

struct place {
	int x, y;
};
queue<place> q;

int main() {
	memset(ans, -1, sizeof(ans));
	cin >> n >> m;
	int mx, my;
	cin >> mx >> my;
	place a;
	a.x = mx;
	a.y = my;
	q.push(a);
	b[mx][my] = 1;
	while (!q.empty()) {
		place now = q.front();
		q.pop();
		for (int i = 0; i <= 7; i++) {
			int x = now.x, y = now.y;
			int u = x + opt[i][0];
			int v = y + opt[i][1];
			if (u >= 1 && u <= n && v >= 1 && v <= m && !b[u][v]) {
				ans[u][v] = ans[x][y] + 1;
				b[u][v] = 1;
				q.push({u, v});
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			/*cout << ans[i][j] << " ";*/printf("%-5d", ans[i][j]);
		}
		printf("\n");
	}
	return 0;
}
2024/7/28 10:29
加载中...