174. Dungeon Game

来源:互联网 发布:国网天津物资 大数据 编辑:程序博客网 时间:2024/06/05 06:12

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)-33-5-1011030-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.

dfs可以做,盲搜,一条路径下来走到终点,如果路径中间出现过负值,取最小的负值取反+1即是这条路径所需要的最小体力值。没有明显的剪枝条件,时间复杂度过大,TLE。

public int calculateMinimumHP(int[][] dungeon){m = dungeon.length;n = dungeon[0].length;dfs(0, 0, 0, dungeon, Integer.MAX_VALUE);return min;}private void dfs(int i, int j, int sum, int[][] dungeon, int curminsum){sum += dungeon[i][j];curminsum = Math.min(curminsum, sum);if (i == m - 1 && j == n - 1){if (curminsum >= 0)min = 1;else{min = Math.min(min, -curminsum + 1);}return;}if (i < m - 1)dfs(i + 1, j, sum, dungeon, curminsum);if (j < n - 1)dfs(i, j + 1, sum, dungeon, curminsum);}


--------------------------------------------------------------------------------------------------------------------------
可以递归dfs就可以考虑能不能DP,按照传统的DP方法,从左上到右下,从起点到终点,做过的DP是求单一的最小值、最大值、步数等等。
这里最主要的区别在于它的限制条件,对于一条路径来说,取决于经过路径点上需要体力的最大值,对于一个位置来说,取决于所有到达这个位置的路径所需要的体力值的最小值。
拿下面的test case作为例子
1-330-20-3-3-3
sum1-211-1(-4)-1(1)-2-4-4(-2)dp13312(5)2(3)355(3)如果做一个sum数组表示到达相应的位置路径上所有点的代数和,那么(2,2)格子就会有两种取值,一个来自上面,一个来自左边,是不是只要让步数最小即dp数组最小,然后保留相应的sum就可以了呢?sum数组(2,3)、(3,2)都有三种取值,这里简略未一一列出,此test case最少步数为3,可以看出上面的做法是不对的,即【让步数最小即dp数组最小,然后保留相应的sum】这里可能导致最优解的丢失,这也是正确的解法dp方向应该从右下出发到左上终止的理由?

摘一段别人的分析:
来自http://blog.csdn.net/u013291394/article/details/51674910

解题思路

一看就是典型的动态规划问题,不过这道题从左上角开始递推不好推,因为要考虑两个因素,一是剩余的血越多越好(血量多可以使得以后的路程中血量更加高一点),行进过程中血量最低值越高越好(这个直接影响到结果),这两个因素都要考虑,且相互影响。其实我们可以从右下角开始推,dp表示如果从该格子出发,最少要多少初始血量。



public int calculateMinimumHP(int[][] dungeon){int m = dungeon.length;int n = dungeon[0].length;int[][] ans = new int[m][n];ans[m - 1][n - 1] = dungeon[m - 1][n - 1] > 0 ? 0: -dungeon[m - 1][n - 1];for (int i = m - 2; i >= 0; i--){ans[i][n - 1] = dungeon[i][n - 1] >= ans[i + 1][n - 1] ? 0: ans[i + 1][n - 1] - dungeon[i][n - 1];}for (int j = n - 2; j >= 0; j--){ans[m - 1][j] = dungeon[m - 1][j] >= ans[m - 1][j + 1] ? 0: ans[m - 1][j + 1] - dungeon[m - 1][j];}for (int i = m - 2; i >= 0; i--)for (int j = n - 2; j >= 0; j--){int down = dungeon[i][j] >= ans[i + 1][j] ? 0 : ans[i + 1][j]- dungeon[i][j];int right = dungeon[i][j] >= ans[i][j + 1] ? 0 : ans[i][j + 1]- dungeon[i][j];ans[i][j] = Math.min(down, right);}return ans[0][0] + 1;}



0 0
原创粉丝点击