174. Dungeon Game

来源:互联网 发布:中央电视台网络客户端 编辑:程序博客网 时间:2024/06/10 01:49

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.




这个题用DP是obvious,但是要从右下角往左上角DP,刚开始我是这样写的

/* * DP:一个位置只保存一个貌似是不行的 * dp表示到达该点时最少需要的血点数,dp1表示到达该点后从右走,dp2从下走 * DFS */public class CopyOfSolution {    public int calculateMinimumHP(int[][] dungeon) {        int m = dungeon.length, n = dungeon[0].length;        int[][] dp1 = new int[m][n], dp2 = new int[m][n];        dp1[m-1][n-1] = Math.max(1, 1 - dungeon[m-1][n-1]);        dp2[m-1][n-1] = Math.max(1, 1 - dungeon[m-1][n-1]);                for(int i=m-2; i>=0; i--) {        dp1[i][n-1] = Integer.MAX_VALUE;        dp2[i][n-1] = Math.max(1, dp2[i+1][n-1] - dungeon[i][n-1]);        }        for(int i=n-2; i>=0; i--) {        dp2[m-1][i] = Integer.MAX_VALUE;        dp1[m-1][i] = Math.max(1, dp1[m-1][i+1] - dungeon[m-1][i]);        }                for(int i=m-2; i>=0; i--) {        for(int j=n-2; j>=0; j--) {        dp1[i][j] = Math.max(1, Math.min(dp1[i][j+1], dp2[i][j+1]) - dungeon[i][j]);        dp2[i][j] = Math.max(1, Math.min(dp1[i+1][j], dp2[i+1][j]) - dungeon[i][j]);        }        }            return Math.min(dp1[0][0], dp2[0][0]);    }}



其实建两个数组完全没有必要,一个就够了,到i,j为止的最优,然后继续往后根据之前的最优寻找最优,还是DP算法不熟

public class Solution {    public int calculateMinimumHP(int[][] dungeon) {        int m = dungeon.length, n = dungeon[0].length;        int[][] dp = new int[m][n];        dp[m-1][n-1] = Math.max(1, 1 - dungeon[m-1][n-1]);                for(int i=m-2; i>=0; i--) {        dp[i][n-1] = Math.max(1, dp[i+1][n-1] - dungeon[i][n-1]);        }        for(int i=n-2; i>=0; i--) {        dp[m-1][i] = Math.max(1, dp[m-1][i+1] - dungeon[m-1][i]);        }                for(int i=m-2; i>=0; i--) {        for(int j=n-2; j>=0; j--) {        dp[i][j] = Math.max(1, Math.min(dp[i][j+1], dp[i+1][j]) - dungeon[i][j]);        }        }            return dp[0][0];    }}




0 0
原创粉丝点击