公式

I - Hammer 2 解説 by en_translator


Let us collectively call the origin, goal, walls, and hammers events.
Let \(L\) be the leftmost event experienced (that is, the origin or goal you arrived, walls you destroyed, or hammers you obtained), and \(R\) be the rightmost one.
Then, we can only consider the case where Takahashi repeat choosing one of the following to perform:

  • Move the next event in the left of \(L\) and perform the event.
  • Move the next event in the right of \(R\) and perform the event.

How can we convert this to DP (Dynamic Programming)?
It is sufficient to compute \(dp[L][R][\)is current position \(L\) or \(R\)?\(]=\{\)the minimum distance required to travel until reaching the goal\(\}\).

Any event other than destroying a wall can be performed, while when breaking a wall, you need to check if the event can be performed (if you have the corresponding hammer.) This is not that difficult: it is sufficient to check if there is an event of picking up the corresponding hammer between \(L\) and \(R\).

The time and spacial complexities are both \(O(N^2)\).

Sample code (C++)

投稿日時:
最終更新: