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.

本题开始的时候做错了,要注意是每个点都要大于0,这道题用dp方法解决,还是自底向上方法解决,当然自顶向下也可以解决。每次每个room还要和1进行比较,其中之前的dp值与当前的-dungeon表示的是残存的血量,而这个残存的血量只保证了到最后的一点时血量剩余1,并不保证每个房间的血量都大于等于1.对于不满足每个房间都大于等于1的dp,要使其值为1.代码如下:

 1 public class Solution {
 2     public int calculateMinimumHP(int[][] dungeon) {
 3         if(dungeon==null||dungeon.length==0||dungeon[0].length==0) return 0;
 4         int m = dungeon.length;
 5         int n = dungeon[0].length;
 6         int[][] dp = new int[m][n];
 7         dp[m-1][n-1] = Math.max(1,1-dungeon[m-1][n-1]);
 8         for(int i=m-2;i>=0;i--){
 9             dp[i][n-1] = Math.max(1,dp[i+1][n-1]-dungeon[i][n-1]);
10         }
11         for(int i=n-2;i>=0;i--){
12             dp[m-1][i] = Math.max(1,dp[m-1][i+1]-dungeon[m-1][i]);
13         }
14         for(int i=m-2;i>=0;i--){
15             for(int j=n-2;j>=0;j--){
16                 int down = Math.max(1,dp[i+1][j]-dungeon[i][j]);
17                 int right = Math.max(1,dp[i][j+1]-dungeon[i][j]);
18                 dp[i][j] = Math.min(down,right);
19             }
20         }
21         return dp[0][0];
22     }
23 }
时间: 2024-10-05 15:46:22

174. Dungeon Game的相关文章

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

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

[email 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

[leedcode 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

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

蠡口174. Dungeon Game

一开始想的是设所求数为x,那么遍历所有可能路径,看最小血量花销. 后来想想对矩阵一顿操作后,返回一个值,而非所有路径的具体值,很有可能是DP.于是往DP的方向想,我们能确定的是救到公主后,骑士的血量至少为1,然后求起始位置是骑士的最低血量可以是多少.这样一想,自然是从右下往左上递推. 1.我们建立dp二维数组:dp[i][j]表示进入房间(i,j)之前骑士血量可以最低是多少. 2.初始边界: 2.1)我们先来想想进入最后的房间之前骑士血量可以最低是多少?最后一个房间可以是: 1)怪兽(即dung