A False Example
We demonstrate a false example to show that there is no top-down, left-right dp solution to this problem.
- We need to test all possible paths from $(0,0)$ to $(i,j)$, find the lowest bound of the path sum along each path, from start to end. (Actually the value is lowest bound + 1, which keeps the knight alive)
- Keep two tables, $dpSum[i][j]$ records the path sum to have $dpMin[i][j]$, which records the lowest bound upon current position along this path.
If we have both $(i-1, j)$ and $(i, j-1)$ for above two tables, do we have $dp[i][j]$ ? The answer is no. Think about it.
Solution
Actually we need to calculate at a bottom-up, right-left order. Let $dp[i][j]$ to be the lowest HP to start from $(i,j)$ to the end, we have:
$$dp[i][j] = \begin{cases} min(dp[i][j-1], dp[i-1][j]) - grid[i][j], & \text{$min(dp[i][j-1], dp[i-1][j]) - grid[i][j] > 0$} \\ 1, & \text{$min(dp[i][j-1], dp[i-1][j]) - grid[i][j] \leq 0$} \end{cases}$$
|
|