174. Dungeon Game
博客专区 > Do0M 的博客 > 博客详情
174. Dungeon Game
Do0M 发表于6个月前
174. Dungeon Game
  • 发表于 6个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

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.
一道普通的动态规划题,迷宫中有BUFF也有DEBUFF,当血量降到0就挂了。从左上角出发,只能向下或向右走,终点在右下角,求怎么走需要的初始血量最少。
int calculateMinimumHP(int** dungeon, int dungeonRowSize, int dungeonColSize) {
    for(int i=dungeonRowSize-1;i>=0;i--){
        for(int j=dungeonColSize-1;j>=0;j--){
            if(i==dungeonRowSize-1&&j==dungeonColSize-1){
                continue;
            }  
            else if(i==dungeonRowSize-1){
                if(dungeon[i][j+1]<0){
                    dungeon[i][j]+=dungeon[i][j+1];
                }
            }
            else if(j==dungeonColSize-1){
                if(dungeon[i+1][j]<0){
                    dungeon[i][j]+=dungeon[i+1][j];
                }
            }
            else{
                int max=dungeon[i+1][j]>=dungeon[i][j+1]?dungeon[i+1][j]:dungeon[i][j+1];
                if(max<0){
                    dungeon[i][j]+=max;
                }
            }
        }
    }
    return dungeon[0][0]>=0?1:1-dungeon[0][0];
}
可以从终点往起点倒推,对迷宫里(除终点外)每一个点计算 从这个点走到终点至少要扣掉多少血,推到起点也就得到结果了。因为从下往上从右往左一行一行遍历,所以每一个点的右和下都在之前算过,因此可以知道下一步是走→好还是走↓好(比如一个值是-5,一个是-2,当然是-2好了,因为-2意味着走这一步之前血量剩下3就可以走到终点而不挂)。知道了下一步怎么走就可以计算当前这个点的数值了:如果下一步是个正数,意味着只要当前角色的血量大于1,就能走到终点而不挂,那就不用改当前点的数值了(如果这一步是BUFF,反正死不了改不改都一样;如果是DEBUFF,那就更不能改,后面能不能活至少要先过了这一步再说,比方这一步有个-10的debuff,下一步是5,角色任然需要11的血量才能走过这一步,因此不改);如果下一步是个负数,那么要在当前数值(也就是BUFF或者DEBUFF的数值)上加上这个负数,意味着从这个点走到终点,至少还要扣掉这么多血。
总之一通DP就阔以了。

这题是HARD,正确率23%,leetcode里算难题了。

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 8
码字总数 24
×
Do0M
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: