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: solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md
+43-29
Original file line number
Diff line number
Diff line change
@@ -60,17 +60,27 @@ Ball b4 is dropped at column 4 and will get stuck on the box between column 2 an
60
60
61
61
## Solutions
62
62
63
+
**Solution 1: Case Discussion + DFS**
64
+
65
+
We can use DFS to simulate the movement of the ball. We design a function $dfs(i, j)$, which represents that the ball starts from the $i$th row and the $j$th column, and finally falls in which column. The ball will get stuck in the following situations:
66
+
67
+
1. The ball is in the leftmost column, and the cell's vane directs the ball to the left.
68
+
2. The ball is in the rightmost column, and the cell's vane directs the ball to the right.
69
+
3. The cell's vane where the ball is located directs the ball to the right, and the vane of the cell adjacent to the right of the ball directs the ball to the left.
70
+
4. The cell's vane where the ball is located directs the ball to the left, and the vane of the cell adjacent to the left of the ball directs the ball to the right.
71
+
72
+
If any of the above situations are met, we can judge that the ball will get stuck and return $-1$. Otherwise, we can continue to recursively find the next position of the ball. Finally, if the ball reaches the last row, we can return the current column number.
73
+
74
+
The time complexity is $O(m \times n)$, and the space complexity is $O(m)$. Where $m$ and $n$ are the number of rows and columns of the array $grid$, respectively.
0 commit comments