博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - Dungeon Game
阅读量:5340 次
发布时间:2019-06-15

本文共 3172 字,大约阅读时间需要 10 分钟。

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
> &dungeon) { 4 int n, m; 5 6 n = (int)dungeon.size(); 7 m = (int)dungeon[0].size(); 8 9 int i, j;10 vector
> dp;11 12 dp.resize(n, vector
(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 };

转载于:https://www.cnblogs.com/zhuli19901106/p/4245014.html

你可能感兴趣的文章
AngularJs练习Demo14自定义服务
查看>>
stat filename
查看>>
关于空想X
查看>>
CF1067C Knights 构造
查看>>
[BZOJ2938] 病毒
查看>>
webstorm修改文件,webpack-dev-server不会自动编译刷新
查看>>
Scikit-learn 库的使用
查看>>
CSS: caption-side 属性
查看>>
python 用数组实现队列
查看>>
认证和授权(Authentication和Authorization)
查看>>
Mac上安装Tomcat
查看>>
CSS3中box-sizing的理解
查看>>
传统企业-全渠道营销解决方案-1
查看>>
Lucene全文检索
查看>>
awk工具-解析1
查看>>
推荐一款可以直接下载浏览器sources资源的Chrome插件
查看>>
CRM product UI里assignment block的显示隐藏逻辑
查看>>
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告...
查看>>
AMH V4.5 – 基于AMH4.2的第三方开发版
查看>>
Mac下安装npm全局包提示权限不够
查看>>