全紫色RE,初学广搜,求助各位大神

P1443 马的遍历

我样例可以过
by xinaocyy @ 2024-02-22 09:43:22


@[xinaocyy](/user/1066932) 一眼盯真,数组开小了。 $n,m\le400$
by WZRYWZWY @ 2024-02-22 09:44:56


@[xinaocyy](/user/1066932) 还有一个问题就是Bfs应该定义为void.
by WZRYWZWY @ 2024-02-22 10:09:47


还有**输出格式**
by 11514zbs @ 2024-02-22 10:29:21


***还有*** 你$n$和$m$写反了。。。
by 11514zbs @ 2024-02-22 10:39:36


RE很多时候就是数组开小了,还有各种越界之类的
by shihan @ 2024-02-22 10:41:13


改动有**点**大。。。 ```cpp #include <cstdio> #include <queue> using namespace std; struct node { int x, y, step; }; int nt[8][2] = {{-1, 2}, {-2, 1}, {2, 1}, {1, 2}, {2, -1}, {-2, -1}, {1, -2}, {-1, -2}}; int m, n, x, y, xx, yy; int book[401][401]; void bfs() { queue<node> que; que.push({x, y, 0}); book[x][y] = 0; while (!que.empty()) { node cur = que.front(); que.pop(); for (int i = 0; i < 8; i++) { xx = cur.x + nt[i][0]; yy = cur.y + nt[i][1]; if (xx < 1 || xx > m || yy < 1 || yy > n || book[xx][yy] != -1) continue; que.push({xx, yy, cur.step + 1}); book[xx][yy] = cur.step + 1; } } } int main() { scanf("%d %d %d %d", &m, &n, &x, &y); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { book[i][j] = -1; } } bfs(); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { printf("%-5d", book[i][j]); } printf("\n"); } return 0; } ```
by 11514zbs @ 2024-02-22 10:55:41


@[xinaocyy](/user/1066932) 求关QWQ
by 11514zbs @ 2024-02-22 10:56:44


|