You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: leetcode/505. The Maze II.md
+102-1Lines changed: 102 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@ There is a ball in a maze with empty spaces and walls. The ball can go through e
6
6
Given the ball's start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.
7
7
8
8
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
The idea of this question is graph traversal. Since we want to find the minimum path from start to destination, so we can use dp(or cache), to save duplicate visiting.
118
+
Meanwhile, we need to use some ideas from the shortest path to update the path.
119
+
* Method 1: bfs
120
+
```Java
121
+
classSolution {
122
+
privatestaticfinalint[][] dir =newint[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
0 commit comments