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. Java当中的异常

    异常:中断了正常指令流的事件,是JVM虚拟机产生的对象 异常是程序运行时产生的,和编译无关 class Test{ public static void main(String args[]){ Sy ...

  2. Linux中JDK环境变量的配置

    在JDK安装好以后,需要进行环境变量的配置 配置目录   /etc/profile 在这个文件的末尾追加 JAVA_HOME=/home/j2sdk1.4.2_11PATH=$PATH:/home/j ...

  3. hdu 5349 MZL's simple problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5349 MZL's simple problem Description A simple proble ...

  4. jquery绑定事件失效的情况(转)

    原文地址:http://www.thinksaas.cn/group/topic/348453/ jQuery中文api地址:http://www.jquery123.com/api/ jQuery官 ...

  5. android开发系列之代码整洁之道

    说起代码整洁之道,想必大家想到更多的是那本经典重构书籍.没错,记得当时自己读那本书的时候,一边结合项目实战,一边结合书中的讲解,确实学到了很多东西,对我自己的编码风格影响极深.随着时间的流逝,书中很多 ...

  6. php正则表达式获取表格内容

    <?php $contents = '<tr class=""> <td>508982</td> <td>08,07,01,0 ...

  7. C++变量的存储类别与作用域

    总结一下C++中变量的存储类别以及变量的作用域. (1)标示符的存储类别决定了标示符在内存中存在的时间(我们可以理解标示符就是确定一个变量的符号,也就是我们所说的变量名) 二:存储类别 (1)静态存储 ...

  8. java 参数化类型

    package com.gxf.collection; import java.util.LinkedList; public class TestForT<T> { private Li ...

  9. java使用ms-dos编译,运行程序

    1.安装好JDK,并配置好环境变量. 2.编辑好源程序,如Test.java public class Test{ public static void main(String[] args){ Sy ...

  10. 结队开发项目—NABC模型

    特点:可以避免食堂的用餐高峰,从而使使用者节约时间 need:很多学生中午下课在食堂吃饭会遭遇用餐高峰,使用这款软件,可以提前订饭,按时送达,从而避免食堂的用餐高峰期. approach:学生可以提前 ...