码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】Dungeon Game 解题报告【Solution】

时间:2015-01-11 23:06:28      阅读:429      评论:0      收藏:0      [点我收藏+]

标签:动态规划   dynamic programming   leetcode   

【题目】

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 1
10 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.
【题意】

有mxn个格子,皇后在右下角,勇士在左上角,现在勇士要去救皇后,每步可以往下或往右走,每个格子里的数字表示勇士到该处时要消耗或增加的魔力,(负数表示消耗,正数表示增加),不论何时只要勇士的魔力小于等于0,勇士立即死掉。问勇士初始至少要有多少魔力?

【解析】

思路:动态规划

用一个二维数组ans[][]表示到每个格子时,勇士到每一步时至少需要的魔力,如ans[i][j]表示勇士在[i, j]处至少需要ans[i][j]魔力才能到达[m, n]救出皇后。

技巧:从[m, n]往回遍历到[1, 1]

动规方程:如果当前位置魔力正且大于等于右边/下边需要的魔力,则该处不需要额外的魔力,否则,勇士到达该处时需有一定的魔力来满足该处和右边/下边需要的魔力。


【Java代码】

public class Solution {
    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);
            }
        }
        
        //要保证勇士活着,至少需要1魔力
        return ans[0][0] + 1;
    }
}


【LeetCode】Dungeon Game 解题报告【Solution】

标签:动态规划   dynamic programming   leetcode   

原文地址:http://blog.csdn.net/ljiabin/article/details/42616291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!