Dungeon Game 174

题目描述:

恶魔把公主关到了地牢(m x n 的grid)的右下角,骑士要从左上角的位置出发到达公主所在的位置解救公主

每个位置都有一个hp值,当骑士到达一个位置时,骑士的hp要加上该位置的hp(当该位置的hp小于0时,hp减少;当该位置的hp大于0时,hp增加)

骑士每次只能往下或者往右走

当骑士的hp值为0是,骑士立即die

计算骑士开始最少要有到少hp,才能到达右下角解救公主(开始位置和公主位置相应的hp也要计算)



题目分析:

求最优解,首先考虑dp

设置状态: d[i][j],表示从(i,j)位置到右下角开始需要最少的hp值

状态转移: d[i][j]=min(d[i+1][j],d[i][j+1])-dungeon[i][j]

从又下角到左上角递推

边界: 对最后一列和最后一行特殊处理

注意:d[i][j] <= 0是无意义的,当d[i][j]<=0时实际上表示的是在该位置,不需要额外的hp值,就可以到达右下角

但是要注意任何时候hp值不能小于等于0,这个时候d[i][j]应该设置为1,来表示不需要额外的hp值

所以对上面状态转移添加一点, 每次计算的d[i][j]之后,d[i][j]的实际值应该是 d[i][j] = d[i][j] > 0 ? d[i][j] : 1

结果:d[0][0]



优化

注意到上面状态转移方程,当前状态只用到了相邻一行的数据,这样可以利用滚动数组压缩空间为 O(n)



代码:

递推

 1 int live(int next,int dun){
 2     int ret=next-dun;
 3     if(ret<=0)ret=1;
 4     return ret;
 5 }
 6
 7 int calculateMinimumHP(vector<vector<int> > &dungeon) {
 8         int lenx=dungeon.size();
 9         if(lenx<=0)return 0;
10         int leny=dungeon[0].size();
11         if(leny<=0)return 0;
12
13         vector<int>d[2];
14
15         int tag=0;
16         for(int j=0;j<leny;j++)d[0].push_back(0),d[1].push_back(0);
17
18         d[tag][leny-1]=live(1,dungeon[lenx-1][leny-1]);
19         for(int j=leny-2;j>=0;j--)
20             d[tag][j]=live(d[tag][j+1],dungeon[lenx-1][j]);
21
22         for(int i=lenx-2;i>=0;i--){
23             tag=1-tag;
24             d[tag][leny-1]=live(d[1-tag][leny-1],dungeon[i][leny-1]);
25
26             for(int j=leny-2;j>=0;j--){
27                 d[tag][j]=live(min(d[1-tag][j],d[tag][j+1]),dungeon[i][j]);
28             }
29         }
30
31         return d[tag][0];
32 }
时间: 2024-08-10 01:59:48

Dungeon Game 174的相关文章

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

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

[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