bfs20求助!#5#10ac

P1443 马的遍历

@[cstcst111](/user/1105374) ``` num[nx][ny]+=ncount; ``` 应该是 ``` num[nx][ny]=ncount+1; ``` 改一下,谢谢
by ikun_god @ 2024-04-02 18:20:27


@[cstcst111](/user/1105374) 搞错了,是`num[nx][ny]=ncount;`
by ikun_god @ 2024-04-02 18:21:28


@[ikun_god](/user/996255) 改了之后全WA了o.O 好奇怪
by cstcst111 @ 2024-04-02 18:28:23


@[cstcst111](/user/1105374) 这道题考点很多,给你我的AC代码 **注意:勿抄!** ```cpp #include <bits/stdc++.h> using namespace std; struct pos { //一个结构体存储x,y两个坐标 int x, y; pos(int ax = 0, int ay = 0) { x = ax; y = ay; } }; const int maxn = 405; queue<pos> Q;//队列 int ans[maxn][maxn];//记录答案,-1表示未访问 int n, m, sx, sy; int walk[8][2] = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1} }; int main() { cin >> n >> m >> sx >> sy; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) ans[i][j] = -1; // 答案都初始化为未访问过 Q.push(pos(sx, sy)); // 使起点入队扩展 ans[sx][sy] = 0; while (!Q.empty()) { // 循环直到队列为空 pos now = Q.front(); // 拿出队首以扩展 Q.pop(); for (int k = 0; k < 8; k++) { int x = now.x + walk[k][0]; int y = now.y + walk[k][1]; // 计算新坐标 x 和 y int d = ans[now.x][now.y]; // d 是目前点的走几步的结果 if (x < 1 || x > n || y < 1 || y > m || ans[x][y] != -1) continue; // 若坐标超过地图范围或者该点已被访问过则无需入队 ans[x][y] = d+1; // 记录下一个点的答案,是目前点多走一步的结果。 Q.push(pos(x, y)); } } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++) printf("%-5d", ans[i][j]); // 输出第 i 行第 j 列答案,整数宽度 5 位 put s(""); // 输出换行 } } ``` **$勿抄代码,害人害己$**
by HAha201205221633 @ 2024-04-02 18:45:24


|