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.

二维DP,从底向上扫。设D[i,j]为走进房间[i,j]之前拥有,并且能够成功到达右下角所需最小HP。由于走进房间[i,j]的下一步要么走进[i,j+1],要么走进[i+1,j],所以选择D[i,j+1],D[i+1,j]中间比较小的那个作为出房间[i,j]的最小HP。所以加上房间本身的补给或是伤害之后:D[i,j] =max(1, min(D[i,j+1],D[i+1,j])-dungeon[i,j])

 public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int m = dungeon.length, n = dungeon[0].length;
int[][] arr = new int[m][n];
arr[m - 1][n - 1] = dungeon[m - 1][n - 1] >= 0 ? 1 : 1 - dungeon[m - 1][n - 1];
for(int i = m - 2; i > -1; i --){
arr[i][n - 1] = Math.max(arr[i + 1][n - 1] - dungeon[i][n - 1], 1);
}
for(int j = n - 2; j > -1; j --){
arr[m - 1][j] = Math.max(arr[m - 1][j + 1] - dungeon[m - 1][j] , 1);
}
for(int i = m - 2; i > -1; i --){
for(int j = n - 2; j > -1; j --){
int before = Math.min(arr[i + 1][j], arr[i][j + 1]);
arr[i][j] = Math.max(before - dungeon[i][j], 1);
}
}
return arr[0][0];
}
}

Dungeon Game的更多相关文章

  1. [LeetCode] Dungeon Game 地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  2. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  3. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  5. leetcode174. Dungeon Game

    // learn from https://discuss.leetcode.com/topic/6912/c-dp-solution ''' class Solution { public: int ...

  6. 【leetcode】Dungeon Game

    Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...

  7. Dungeon Game ——动态规划

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  8. Java for LeetCode 174 Dungeon Game

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  9. LeetCode Dungeon Game

    原题链接在这里:https://leetcode.com/problems/dungeon-game/ 这是一道DP题,保存当前格到右下格所需要的最小体力,m*n的dp数组保存. 更新是Math.mi ...

  10. Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...

随机推荐

  1. Requests:Python HTTP Module学习笔记(一)(转)

    Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...

  2. entityFramework使用 codefirst

    新建项目 用nuget安装entityFramework,Install-Package Entityframework 建一个model和context //[Table("Custome ...

  3. IBM MQ扩大队列最大消息长度

    要设置MQ的最大消息长度,需要考虑同时设置队列管理,队列以及通道的最大消息长度. 具体操作如下: runmqsc 队列管理器名称 alter qmgr maxmsgl(10000000) 1 : al ...

  4. golang的哪些坑爷事: package实践

    在golang中package是个困惑的概念, 特别是package还可以与folder不同名, 委实让我恶心了一把. 关于golang的package的最佳实践: package is folder ...

  5. go语言值得学习的开源项目推荐

    谷歌官方维护了一个基于go语言的开源项目列表: https://github.com/golang/go/wiki/Projects 其中有非常多的优秀项目值得学习,有几百行代码适合新手阅读的项目,也 ...

  6. mongoDB 3.0 安全权限访问控制

    MongoDB3.0权限,啥都不说了,谷歌百度出来的全是错的.先安装好盲沟,简单的没法说. 首先,不使用 —auth 参数,启动 mongoDB: mongodb-linux-i686-3.0.0/b ...

  7. iOS关于打包出错

    运行没问题,有可能是自动打包编译脚本的存在,删除掉即可.

  8. simplexml_load_string 解析gb2312编码的xml

    <?php header('Content-type:text/html;charset=UTF-8'); $url = 'http://www.xxx.com/text.xml'; $cont ...

  9. 可综合风格的VerilogHDL模块实例

    1.赋值语句:assign{cout,sum}=a+b+cin; 2.利用电平敏感的always块设计组合逻辑电路 3.always块中如果含有局部变量,就必须在begin后加模块名,是必须加,同样的 ...

  10. canvas圆环进度

    CSS: <div class="circle"> <p><span id="loadedNum">0</span&g ...