leetcode[174]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.

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.

思路:典型的动态规划,从右下角往左上角走,先初始化右下角及最后一行,最后一列,

然后计算f[i][j](f[i][j]表示进入第i行第j列的格子所需要的能量值):

1)right=dungeon[i][j]<f[i][j+1]?f[i][j+1]-dungeon[i][j]:1;

2)down=dungeon[i][j]<f[i+1][j]?f[i+1][j]-dungeon[i][j]:1;

3)f[i][j]=right<down?right:down;

class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
    if(dungeon.empty())return 1;
    int m=dungeon.size();
    if(dungeon[0].empty())return 1;
    int n=dungeon[0].size();
    vector<vector<int> >  f(m,vector<int> (n,0));
    f[m-1][n-1]=dungeon[m-1][n-1]<0?1-dungeon[m-1][n-1]:1;
    for(int i=m-2;i>=0;i--)
        f[i][n-1]=dungeon[i][n-1]<f[i+1][n-1]?f[i+1][n-1]-dungeon[i][n-1]:1;
    for(int j=n-2;j>=0;j--)
        f[m-1][j]=dungeon[m-1][j]<f[m-1][j+1]?f[m-1][j+1]-dungeon[m-1][j]:1;
    for(int i=m-2;i>=0;i--)
    {
        for(int j=n-2;j>=0;j--)
        {
            int right=dungeon[i][j]<f[i][j+1]?f[i][j+1]-dungeon[i][j]:1;
            int down=dungeon[i][j]<f[i+1][j]?f[i+1][j]-dungeon[i][j]:1;
            f[i][j]=right<down?right:down;
        }
    }
    return f[0][0];
}
};
时间: 2024-12-22 18:28:39

leetcode[174]Dungeon Game的相关文章

[动态规划] leetcode 174 Dungeon Game

problem:https://leetcode.com/problems/dungeon-game 看了这道题的tag我用了二分 + 简化dp(只需求特定血量能否达到)来做,能过但是速度好慢.一看评论区发现大家都是用纯dp过的,我哭了. class Solution { public: int m, n; bool canSave(vector<vector<int>>& dungeon, int life) { vector<vector<int>&g

Java for LeetCode 174 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 wa

leetcode 174. Dungeon Game 地牢游戏 --------- java

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 wa

LeetCode 174. Dungeon Game(DP)

题目 题意:每个格子里都有数字,负数代表你会少血,正数代表你会加血,当你的血量为0的时候就死了,从左上角出发,到右下角,问你一开始最少的血量是多少.整个过程中不能有血量为0的情况. 题解:只能走下或者走右.这种有向无环图,八成都是动态规划.但是如果从左上角开始规划,有很多情况要考虑.从右下角开始规划,才可以. dp[i][j]代表从i,j到右下角,最低需要多少血量. class Solution { public: int dp[1005][1005]; int calculateMinimum

leetcode 174. 地下城游戏 解题报告

leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主. 骑士的初始健康点数为一个正整数.如果他的健康点数在某一时刻降至 0 或以下,他会立即死亡. 有些房间由恶魔守卫,因此骑士在进入这些房间时会失去健康点数(若房间里的值为负整数,则表示骑士将损失健康点数):其他房间要么是空的(房间里的值为 0),要么包含增加骑士健康点数的魔

[email&#160;protected] [174] Dungeon Game (Dynamic Programming)

https://leetcode.com/problems/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 positione

174 Dungeon Game 地下城游戏

一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数表示的初始健康点.如果他的健康点在任何时候降至 0 或以下,他会立即死亡.有些房间由恶魔守卫,因此骑士在进入这些房间时失去健康点(负整数):其他房间要么是空的(0),要么包含增加骑士身体健康的魔法球(正整数).为了尽快到达公主,骑士决定只会每次向右或向下移动一步.写一个函数来确定骑士的最低初始健康点

lc 174. Dungeon Game

https://leetcode.com/problems/dungeon-game/ 一个二维矩阵n*m做的地图上,每个都有或正或负的整数,对一从左上到右下的最短路径(每步只能向右或者下)我们走的同时会记录当前位置的累加和s,所以一条路会有n+m-2个位置,所有这些和的最小值是该路径的价值,求价值最大的路. 一个奇怪的dp,这是首先想到的. 因为这个题目让我费解了好一段时间,所以记录思考过程比记录最终解法更有意义. dp会有一个对于最好状态的记录,这个最好状态记录了之后,其怎么取得的不重要,只

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

[题目] 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 h