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/0800-0899/0838.Push Dominoes/README_EN.md
+54-62Lines changed: 54 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,34 +69,26 @@ tags:
69
69
70
70
### Solution 1: Multi-Source BFS
71
71
72
-
Treat all dominoes initially pushed (`L` or `R`) as **sources**, which simultaneously propagate their forces outward. Use a queue to perform BFS layer by layer (0, 1, 2, ...):
72
+
Treat all initially pushed dominoes (`L` or `R`) as **sources**, which simultaneously propagate their forces outward. Use a queue to perform BFS layer by layer (0, 1, 2, ...):
73
73
74
-
1.**Modeling**
74
+
We define $\text{time[i]}$ to record the first moment when the _i_-th domino is affected by a force, with `-1` indicating it has not been affected yet. We also define $\text{force[i]}$ as a variable-length list that stores the directions (`'L'`, `'R'`) of forces acting on the domino at the same moment. Initially, push all indices of `L/R` dominoes into the queue and set their `time` to 0.
75
75
76
-
-`time[i]` records the first moment when the _i_-th domino is affected by a force, with `-1` indicating it has not been affected yet.
77
-
-`force[i]` is a variable-length list that stores the directions (`'L'`, `'R'`) of forces acting on the domino at the same moment.
78
-
- Initially, push all indices of `L/R` dominoes into the queue and set their `time` to 0.
76
+
When dequeuing index _i_, if $\text{force[i]}$ contains only one direction, the domino will fall in that direction $f$. Let the index of the next domino be:
79
77
80
-
2.**Propagation Rules**
78
+
$$
79
+
j =
80
+
\begin{cases}
81
+
i - 1, & f = L,\\
82
+
i + 1, & f = R.
83
+
\end{cases}
84
+
$$
81
85
82
-
- When dequeuing index _i_, if `force[i]` contains only one direction, the domino will fall in that direction `f`.
83
-
- Let the index of the next domino be:
86
+
If $0 \leq j < n$:
84
87
85
-
$$
86
-
j=\begin{cases}
87
-
i-1,& f=L\\[2pt]
88
-
i+1,& f=R
89
-
\end{cases}
90
-
$$
88
+
- If $\text{time[j]} = -1$, it means _j_ has not been affected yet. Record $\text{time[j]} = \text{time[i]} + 1$, enqueue it, and append $f$ to $\text{force[j]}$.
89
+
- If $\text{time[j]} = \text{time[i]} + 1$, it means _j_ has already been affected by another force at the same "next moment." In this case, append $f$ to $\text{force[j]}$, causing a standoff. Subsequently, since $\text{len(force[j])} = 2$, it will remain upright.
91
90
92
-
If $0 \leq j < n$:
93
-
94
-
- If `time[j] == -1`, it means _j_ has not been affected yet. Record `time[j] = time[i] + 1`, enqueue it, and append `f` to `force[j]`.
95
-
- If `time[j] == time[i] + 1`, it means _j_ has already been affected by another force at the same "next moment." In this case, append `f` to `force[j]`, causing a standoff. Subsequently, since `len(force[j]) == 2`, it will remain upright.
96
-
97
-
3.**Final State Determination**
98
-
99
-
- After the queue is emptied, all positions where `force[i]` has a length of 1 will fall in the corresponding direction, while positions with a length of 2 will remain as `.`. Finally, concatenate the character array to form the answer.
91
+
After the queue is emptied, all positions where $\text{force[i]}$ has a length of 1 will fall in the corresponding direction, while positions with a length of 2 will remain as `.`. Finally, concatenate the character array to form the answer.
100
92
101
93
The complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of dominoes.
0 commit comments