G - AtCoder Tour 解説 by en_translator
In an optimal solution, one can assume that Takahashi stays at his current cell only at a cell with the maximum \(A_{i,j}\) among those he passes through. (Otherwise, he can replace an action of staying another cell with an action of staying that cell to improve the score.) Thus, one can assume that once he stays at a cell, he can keep staying there indefinitely.
Let \((G_i, G_j)\) be the final cell after his procedure end. By the fact above, we can assume that he travels from \((S_i, S_j)\) to \((G_i, G_j)\), and then stays at \((G_i, G_j)\) zero or more times.
Consider the travel from \((S_i, S_j)\) to \((G_i, G_j)\). As shown above, he never stays at his current cell; one can even show that he never passes through the same cell twice. This is because, if he passes through \((p,q)\) twice, he can remove the process of exiting and going back to \((p, q)\), and stay at \((G_i, G_j)\) more times, without decreasing his score.
Therefore, one can consider paths from \((S_i, S_j)\) to \((G_i, G_j)\) of length not greater than \(HW\).
Therefore, one can find the answer with a straightforward DP (Dynamic Programming).
Specifically, one can evaluate \(dp_{i,j,k} =\) the maximum total fun value after making \(i\) moves to end up at \((j,k)\). The answer is \(\max ((K - i) A_{j,k} + dp_{i,j,k})\).
Since we only need to evaluate it for \(i \leq HW\), the DP above runs in a total of \(O((HW)^2)\) time. Beware of cases where \(K \leq HW\).
投稿日時:
最終更新: