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.

Note:

  • 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.

在迷宫中每步都要加血和减血,问从左上角到右下角最少的初始血量是多少。

二,题目分析

动态规划+深度搜索

骑士从左上角出发,每次只能向右走或向下走,最后到达右下角见到公主
地图中每个位置的值代表骑士要遭遇的事情。如果是负数,说明此处有怪兽,骑士要损失血量。如果是非负数,代表此处有血瓶,能让骑士回血。
骑士从左上角到右下角的过程中,走到任何一个位置时,血量不能少于1.
dp[i+1][j]是骑士选择当前向下走并最终到达右下角的血量要求,dp[i][j+1]是向右走的要求。
那么骑士在当前位置加血或扣血之后的血量只要等于dp[i][j+1]即可,骑士在加血或扣血之前的血量要求就是dp[i][j+1]-map[i][j]。同时,血量要随时不少于1,所以向右的要求是max{dp[i][j+1]-map[i][j],1}

所以解题思路为,到达终点的血量要求我们很容易求出来,从终点开始往回推也很容易,就需要一个动态规划帮我们解决所有问题。首先初始化边界的值,因为他们只能往右走或往下走,所以容易确定, 然后求出地图中间值的需求。

三,题目解答

class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        if(dungeon == null)
            return 1;
        int row = dungeon.length;
        int col = dungeon[0].length;
        int[][] dp = new int[row][col];
        dp[row-1][col-1] = dungeon[row-1][col-1] > 0? 1: 1 - dungeon[row-1][col-1];
        if(row > 1){
            for(int i = row-2; i >= 0; i--){
                dp[i][col-1] = Math.max(dp[i+1][col-1] - dungeon[i][col-1],1);
            }
        }
        if(col > 1){
            for(int i = col-2; i >= 0; i--){
                dp[row-1][i] = Math.max(dp[row-1][i+1] - dungeon[row-1][i],1);
            }
        }
        if(row == 1 || col == 1){
            return dp[0][0];
        }
        int right = 0;
        int down = 0;
        for(int i = row-2; i >= 0; i--){
            for(int j = col-2; j >= 0; j--){
                right = Math.max(dp[i][j+1] - dungeon[i][j], 1);
                down = Math.max(dp[i+1][j] - dungeon[i][j], 1);
                dp[i][j] = Math.min(right, down);
            }
        }
        return dp[0][0];
        
    }
}

 

四,题目回忆

注意这道题和路径求解的区别,这道题是地图中每个区域都有相应的解,我们所要做的是通过动态规划把解找出来。不过我们也可以适当变型,如果给定了骑士的初始血量,求解从左上角到能够到达右下角的所有路径数。我们也可以通过类似的想法得到答案,因为我们也可以看成地图上所有区域都有对应的值,只不过具有淘汰机制(把淘汰的区域值设为-1),还是先初始化边界,然后填充地图中心的区域并判断该区域是否有用即可。

由此可见地图动态规划题往往需要辅助空间帮助计算,理解辅助空间的含义过后,然后先计算边界值再填充区域内的值,并在循环过程中实时更新判断自身的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值