← 返回 meta 的题目列表AI Coding Maze: Fix BFS by Adding Visited Set
类型:online_judge
Given a maze/grid graph, run BFS from S to E to find the shortest path length. The provided code has a bug: it does not maintain visited correctly, causing repeated enqueues, infinite loops, or TLE.
Fix the code by:
maintaining a visited (or dist) structure in the correct place so each state is processed at most once;
returning the shortest number of steps; return -1 if unreachable.
Constraints: R,C <= 200.
Example:
S..
##.
..E
Output: 4 (4-directional moves).
Example
Input
3 3
S..
##.
..E
Output
4