C - Destruction of Walls 解説 by evima
Let \((i,j)\) denote the room at the \(i\)-th row from the top and \(j\)-th column from the left.
We consider the number of ways to choose walls satisfying the condition by case analysis on \(K\).
When \(K<H+W-2\)
Since at least \(H+W-2\) moves are required to go from \((1,1)\) to \((H,W)\) in the shortest path, there are \(0\) ways.
When \(K=H+W-2\)
This equals the number of ways to move from \((1,1)\) to \((H,W)\) in the shortest path, which is \(\binom{H+W-2}{H-1}\).
When \(K=H+W-1\)
This equals the number of ways to choose a shortest path from \((1,1)\) to \((H,W)\) and one wall not included in that path, which is \(\binom{H+W-2}{H-1}\times(H(W-1)+(H-1)W-(H+W-2))\).
When \(K=H+W\)
The shortest path length is either \(H+W-2\) or \(H+W\).
Consider the case where the shortest path length is \(H+W-2\).
When we choose \(H+W-2\) walls used in a shortest path from \((1,1)\) to \((H,W)\) and two other walls, only those with exactly two shortest paths are double-counted.
The choices with two shortest paths are those where there exists \((i,j)\) such that all walls in
\(\\(i,j)\rightarrow(i,j+1)\rightarrow(i+1,j+1)\\
(i,j)\rightarrow(i+1,j)\rightarrow(i+1,j+1)\)
are chosen. This corresponds one-to-one with choosing a shortest path from \((1,1)\) to \((H-1,W-1)\) and one room included in that path, giving \(\binom{H+W-4}{H-2}\times(H+W-3)\) ways.
Consider the case where the shortest path length is \(H+W\).
This involves moving exactly once in the direction away from \((H,W)\) in exactly one of the vertical or horizontal directions.
Consider the case where this direction is vertical.
When the shortest path length is \(H+W\), the movements before and after this move must be horizontal.
If we replace these H-V-H movements with V’, we can see that this corresponds to arranging \(H+1\) V and \(W-3\) H movements, then replacing one of the \(2\)-nd to \(H\)-th V movements with V’.
The case where the direction away is horizontal can be solved similarly.
投稿日時:
最終更新: