2015.1.23 19:38
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.
Solution:
This problem should be one about dynamic programming. Let dp[i][j] be the minimal HP necessary to reach the destination (n - 1, m - 1), if you start from the position (i, j). Then the answer lies in dp[0][0], which will be calculated backward from (n - 1, m - 1) all the way back to (0, 0).
Try to understand the meaning of min(), even if you can pass a position (i, j), you'll always need at least 1 point of HP to stay alive.
Time and space complexity are both O(n * m), though space can be reduced to O(m) with extra coding, you know how, right?
Accepted code:
1 class Solution { 2 public: 3 int calculateMinimumHP(vector<vector<int> > &dungeon) { 4 int n, m; 5 6 n = (int)dungeon.size(); 7 m = (int)dungeon[0].size(); 8 9 int i, j; 10 vector<vector<int> > dp; 11 12 dp.resize(n, vector<int>(m)); 13 dp[n - 1][m - 1] = min(1 - dungeon[n - 1][m - 1]); 14 for (i = n - 2; i >= 0; --i) { 15 dp[i][m - 1] = min(dp[i + 1][m - 1] - dungeon[i][m - 1]); 16 } 17 for (j = m - 2; j >= 0; --j) { 18 dp[n - 1][j] = min(dp[n - 1][j + 1] - dungeon[n - 1][j]); 19 } 20 21 int s1, s2; 22 for (i = n - 2; i >= 0; --i) { 23 for (j = m - 2; j >= 0; --j) { 24 s1 = dp[i][j + 1]; 25 s2 = dp[i + 1][j]; 26 dp[i][j] = min((s1 < s2 ? s1 : s2) - dungeon[i][j]); 27 } 28 } 29 s1 = dp[0][0]; 30 for (i = 0; i < n; ++i) { 31 dp[i].clear(); 32 } 33 dp.clear(); 34 35 return s1; 36 } 37 private: 38 int min(int x) { 39 return x <= 0 ? 1 : x; 40 } 41 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)