BFS Shortest Path
2026/3/9小于 1 分钟
BFS Shortest Path
题面
在无权 2D 栅格中用 BFS 求起点到终点的最短步数(四联通,0 可通行,1 为障碍)。不可达返回 −1。
Implementation Requirements
- Use only native features (external libraries are not permitted)
- The solve function signature must remain unchanged
- 返回最短路径长度(或 −1)
Examples
见页面两组示例(4×4 可达为 6;3×3 不可达为 −1)。
Constraints
- 1 ≤ rows, cols ≤ 1000;起终点在边界内且为 0;起终点可相同(返回 0)
- Performance: rows=500, cols=500