本文解析bfs在二维迷宫中无法找到目标值9的根本原因,重点指出使用无分隔符的字符串拼接(如"110")作为哈希键会导致不同坐标(如(1,10)与(11,0))哈希冲突,进而跳过合法节点;同时提供健壮、简洁的修复方案。
在实现基于广度优先搜索(BFS)的二维迷宫路径查找时,一个看似微小却极具破坏性的错误常导致算法“看似运行正常”却始终无法抵达目标(如值为 9 的终点)。问题核心并非逻辑结构或边界判断,而在于节点去重机制的设计缺陷。
原代码中使用以下方式生成访问标记键:
String ps = Integer.toString(p.x) + Integer.toString(p.y);
该方式缺少分隔符,造成严重歧义。例如:
✅ 正确做法:强制引入唯一分隔符(如 ":", "," 或 "_") ❌ 错误示例:"110"、"12345"(无法区分 (12,345) 与 (123,45))
改用 Set
更语义化、零冲突、无需字符串操作:
public static Setvisited = new HashSet<>(); // ... Point curr = new Point(p.x, p.y); visited.add(curr); // 自动去重(需确保 Point 正确重写 equals/hashCode)
若坚持字符串键,请严格使用带分隔符格式
String key = p.x + "," + p.y; // 如 "1,10" ≠ "11,0"
if (!visited.contains(key)) {
visited.add(key);
q.add(new Box(p.x, p.y, p));
}优化数据结构:用 Set 替代 Map
原 HashMap
本质是模拟集合,既冗余又易错。直接使用 HashSet
public static Queueq = new LinkedList<>(); public static Set visited = new HashSet<>(); // ← 改为 Set public static void searchPath(int[][] maze, int x, int y, ArrayList path) { q.add(new Box(x, y, null)); visited.add(x + "," + y); // ← 入队即标记 while (!q.isEmpty()) { Box p = q.poll(); if (maze[p.y][p.x] == 9) { System.out.println("target found!"); getPath(p, maze, path); return; } // 四方向扩展(以右为例,其余同理) int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1}; for (int i = 0; i < 4; i++) { int nx = p.x + dx[i], ny = p.y + dy[i]; String nextKey = nx + "," + ny; if (isFree(maze, nx, ny) && !visited.contains(nextKey)) { visited.add(nextKey); q.add(new Box(nx, ny, p)); } } } System.out.println("exited without finding target"); }
通过修正哈希键设计,BFS 将真正实现“无遗漏、无重复”的层级遍历,确保目标可达时必被发现。记住:BFS 的正确性,始于每一个坐标的唯一身份标识。