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.

基本思路:

动态规划

设health[i][j]  为走进dungeon[i][j]的初始血量,且该个血量将能维持到骑士足以走完右下角。

已知条件:骑士走完右下角至少要剩一滴血。即health[m][n-1] = 1。

m为dungeon行数。

也能够设health[m-1][n] = 1。

此值表示走完右下角的剩余血量。

同一时候也是从该右下角向右,或者向下。走到还有一格时的初始血量。 当然此两格是虚拟的,地牢中不存在。或者形象的说,从右下角向右或者向下走出地牢后,剩余的血量。

从此点。能够倒推出health[0][0]。

骑士仅仅能向右,向下右移动。  要知道当前位置的初始血量,仅仅须要知道其右和其下的初始血量,就能够反推出。

health[i][j] = min(health[i+1][j], health[i[j+1])  - dungeon[i][j]

因为骑士要时刻保持血量至少为1. 上面能够改为:

health[i][j] = max(1, min(health[i+1][j], health[i[j+1])  - dungeon[i][j])

因为递推时仅仅须要其右和其下,两个位置, 能够使用滚动数组。用一维替换掉二维数组。


class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
if (dungeon.empty() || dungeon[0].empty())
return 0;
const int m = dungeon.size();
const int n = dungeon[0].size();
vector<int> health(n+1, INT_MAX);
health[n-1] = 1;
for (int i=m-1; i>=0; i--) {
for (int j=n-1; j>=0; j--) {
health[j] = max(1, min(health[j], health[j+1]) - dungeon[i][j]);
}
}
return health[0];
}
};

最新文章

  1. 通过WebStorm上传代码至github
  2. loss function
  3. IMap 对map的功能的强化
  4. noip2006 2^k进制数
  5. ZMQ 在linux进程 和分布式之间的通信
  6. oracle decode(nvl(estimate_qty,0),0,1,estimate_qty) 函數
  7. Cannot change network to bridged: There are no un-bridged host network adapters解决方法
  8. MySQL各个版本区别
  9. Java正则表达式之语法规则
  10. POJ 3687 Labeling Balls【拓扑排序 优先队列】
  11. 导出Execel
  12. VS2010的openssl源码编译方法
  13. Handler机制原理图、源码、使用!!!!!
  14. 解决eclipse中maven出现的Failure to transfer XXX.jar的问题
  15. C语言中,隐藏结构体的细节
  16. Ubuntu上使用Web QQ
  17. 201521123088 《Java程序设计》第14周学习总结
  18. python 爬取糗事百科 gui小程序
  19. Centos7安装搭建NTP服务器和NTP客户端同步时间
  20. 洛谷AT2342 Train Service Planning(思维,动态规划,珂朵莉树)

热门文章

  1. Sqoop hive 和mysql 交互 完整案例
  2. 扩增子统计绘图1箱线图:Alpha多样性
  3. 梦想MxWeb3D协同设计平台 2018.10.12更新
  4. 调用微信扫一扫功能,踩坑&#39;invalid signature&#39;
  5. Spring Boot 与ElasticSearch
  6. Git 分支使用
  7. [angular1.6]Error: &quot;transition superseded&quot; ui-router 在angular1.6 报错误问题解决
  8. UVA - 208 Firetruck(并查集+dfs)
  9. JDBC插入数据时中文变为问号的解决方法
  10. Android jdbc连接mysql报错解决方案 (Communications link failure)