godogod @ 2024-08-07 15:08:40
#include<bits/stdc++.h>
using namespace std;
int n, m, nx, ny;
int vis[405][405];
struct point {
int x, y;
} que[10005];
bool jud[405][405];
int head, tail;
int flag = 0;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int main() {
cin >> n >> m >> nx >> ny;
memset(vis, -1, sizeof(vis));
memset(jud, true, sizeof(jud));
vis[nx][ny] = 0;
head = 1, tail = 1;
que[1].x = nx;
que[1].y = ny;
while (head <= tail) {
int p = head;
for (int i = 0; i < 8; i++) {
int xx = que[p].x + dx[i];
int yy = que[p].y + dy[i];
if (xx >= 1 && xx <= m && yy >= 1 && yy <= n && vis[xx][yy] == -1) {
tail++;
vis[xx][yy] = vis[que[p].x][que[p].y] + 1;
que[tail].x = xx;
que[tail].y = yy;
jud[xx][yy] = false;
}
}
head++;
}
for (int a = 1; a <= n; a++) {
for (int b = 1; b <= m; b++) {
if (b == 1) cout << vis[a][b];
else cout << setw(4) << vis[a][b];
}
cout << endl;
}
return 0;
}
by Mzh2012 @ 2024-08-07 15:16:46
首先
if (xx >= 1 && xx <= m && yy >= 1 && yy <= n && vis[xx][yy] == -1) {
改成
if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && vis[xx][yy] == -1) {
然后你就90分了
by Mzh2012 @ 2024-08-07 15:19:00
@godogod 然后WA一个点应该是你的马的初始位置有问题吧 也有可能不是。
by godogod @ 2024-08-07 15:44:39
@Mzh2012 试了那个点直接输了一堆-1
by Mzh2012 @ 2024-08-07 15:47:34
@godogod 我也下了那个点。但还没找到错误原因。
by Mzh2012 @ 2024-08-07 15:48:13
@godogod 你这种搜索发式我没见过。
by godogod @ 2024-08-07 15:54:48
@Mzh2012 手动队列(
by Mzh2012 @ 2024-08-07 16:02:53
@godogod 哦