首页 > 代码库 > Leetcode: Dungeon Game

Leetcode: Dungeon Game

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.

这是典型的思想大于行动的题目,想好了,就寥寥几段代码就搞定了。

这道题显然是一道DP的题目,DP题目关键就是两点:1. 维护量; 2. 递归方程

dp[i][j]表示进入这个格子后保证knight不会死所需要的进入这个格子之前最小HP。以最后格子为例,如果格子的值为负,那么进入这个格子之前knight需要有的最小HP是-dungeon[i][j] + 1.如果格子的值非负,那么最小HP需求就是1.

这里可以看出DP的方向是从最右下角开始一直到左上角。首先dp[m-1][n-1] = Math.max(1, -dungeon[m-1][n-1] + 1).

然后需要generalize,可以发现上面这个式子最右边那个1表示的是:进入这个格子之后的HP值。那么对于一般格子,这里就可以替换为进入下一个格子所需最小HP。可以往下走也可以往右走。

因为是从最右下角的格子往前面推,所以dp[i][j]表示的进入某个格子的最小HP是能够保证他走到最右下角的。也是因为有了这个保证,我们选往右还是往下根据dp[i+1][j] < dp[i][j+1] ?

递归方程是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 }

 

Leetcode: Dungeon Game