Leetcode: Dungeon Game, leetcodedungeon
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.-2 (K) -3 3-5 -10 110 30 -5 (P)Notes:The knight's health has no upper bound.Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
This is a typical idea that exceeds the question of action. If you have a good idea, you can get it done with just a few pieces of code.
This question is obviously a DP question. The key points of the DP question are: 1. Maintenance volume; 2. recursive equation.
Dp [I] [j] indicates that after entering this grid, knight will not die and the minimum HP is required before entering this grid. Take the last grid as an example. If the value of the grid is negative, the minimum HP required by knight before entering the grid is-dungeon [I] [j] + 1. if the value of the grid is not negative, the minimum HP requirement is 1.
Here we can see that the direction of DP is from the bottom right corner to the top left corner. Dp [M-1] [n-1] = Math. max (1,-dungeon [M-1] [n-1] + 1 ).
Then generalize is required. We can find that the 1 on the rightmost side of the subscriber above indicates the hpvalue after entering the lattice. For the general lattice, replace it with the minimum HP required to enter the next lattice. You can go down or right.
Because the cursor is pushed from the bottom right pane to the front, dp [I] [j] indicates that the smallest HP entry into a grid can be ensured to go to the bottom right corner. Because of this guarantee, do we select to the right or down based on dp [I + 1] [j] <dp [I] [j + 1]?
The recursive equation is dp [I] [j] = Math. max (1,-dungeon [I] [j] + Math. min (dp [I + 1] [j], dp [I] [j + 1]).
1 public class Solution { 2 public int calculateMinimumHP(int[][] dungeon) { 3 int m = dungeon.length; 4 int n = dungeon[0].length; 5 if (m == 0) return 0; 6 int[][] res = new int[m][n]; 7 res[m-1][n-1] = Math.max(1, -dungeon[m-1][n-1]+1); 8 for (int i=m-2; i>=0; i--) { 9 res[i][n-1] = Math.max(1, -dungeon[i][n-1]+res[i+1][n-1]);10 }11 for (int j=n-2; j>=0; j--) {12 res[m-1][j] = Math.max(1, -dungeon[m-1][j]+res[m-1][j+1]);13 }14 for (int i=m-2; i>=0; i--) {15 for (int j=n-2; j>=0; j--) {16 res[i][j] = Math.max(1, -dungeon[i][j]+Math.min(res[i+1][j], res[i][j+1]));17 }18 }19 return res[0][0];20 }21 }