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.

解题思路:

dp问题,有两种思路:

思路一:dp[i][j]表示从起点到dungeon[i][j]所需的最小血量,但是这种思路递推方程非常不好写

思路二:dp[i][j]表示从dungeon[i][j]到终点所需的最小血量,使用一维数组即可,递推方程

dp[j] = dungeon[i][j] >= Math.min(dp[j + 1], dp[j]) - 1 ? 1: Math.min(dp[j + 1], dp[j]) - dungeon[i][j]

当然,也可以直接用dungeon[i][j]数组替代dp[]数组,JAVA实现如下:

    public int calculateMinimumHP(int[][] dungeon) {
int[] dp = new int[dungeon[0].length];
dp[dungeon[0].length - 1] = dungeon[dungeon.length - 1][dungeon[0].length - 1] >= 0 ? 1
: 1 - dungeon[dungeon.length - 1][dungeon[0].length - 1];
for (int i = dungeon[0].length - 2; i >= 0; i--)
dp[i] = dungeon[dungeon.length - 1][i] >= dp[i + 1] - 1 ? 1
: dp[i + 1] - dungeon[dungeon.length - 1][i];
for (int i = dungeon.length - 2; i >= 0; i--) {
dp[dungeon[0].length - 1] = dungeon[i][dungeon[0].length - 1] >= dp[dungeon[0].length - 1] - 1 ? 1
: dp[dungeon[0].length - 1]
- dungeon[i][dungeon[0].length - 1];
for (int j = dungeon[0].length - 2; j >= 0; j--)
dp[j] = dungeon[i][j] >= Math.min(dp[j + 1], dp[j]) - 1 ? 1
: Math.min(dp[j + 1], dp[j]) - dungeon[i][j];
}
return dp[0];
}

Java for LeetCode 174 Dungeon Game的更多相关文章

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

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

  2. [LeetCode] 174. Dungeon Game 地牢游戏

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

  3. leetcode@ [174] Dungeon Game (Dynamic Programming)

    https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...

  4. Java实现 LeetCode 174 地下城游戏

    174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来 ...

  5. Leetcode#174 Dungeon Game

    原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...

  6. [leetcode]174. Dungeon Game地牢游戏

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

  7. LeetCode 174. Dungeon Game (C++)

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

  8. leetcode 174. 地下城游戏 解题报告

    leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...

  9. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. Tarjan算法

    SCC即强连通分量,即一个图的子图,其中的点能相互到达,全称是strongly connected component. Tarjan算法是用来找出图的SCC. 伪代码 int index = 0; ...

  2. iOS-编译简单静态库初探

    首先声明,我写的这些网上都有更详细的内容,在这里只是写下我自己总结的一些重要内容,具体步骤如下: 事先准备:新建工程-Framework & Library - Cocoa Touch Sta ...

  3. 【BZOJ-3545&3551】Peaks&加强版 Kruskal重构树 + 主席树 + DFS序 + 倍增

    3545: [ONTAK2010]Peaks Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1202  Solved: 321[Submit][Sta ...

  4. POJ3749 破译密码

    Description 据说最早的密码来自于罗马的凯撒大帝.消息加密的办法是:对消息原文中的每个字母,分别用该字母之后的第5个字母替换(例如:消息原文中的每个字母A都分别替换成字母F).而你要获得消息 ...

  5. hdu acmsteps 2.1.8 Leftmost Digit

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. JBoss7.1配置外网访问

    在JBoss7.1目录jboss-as-7.1.1.Final/standalone/configuration下找到standalone.xml,找到以下的节点,在尝试了以下两种方法: 1. < ...

  7. 架构(Architecture)和框架(Framework)杂谈

    1. 架构和框架的设计层次不同       类似于硬件设计,软件设计也分为不同的层次.典型的软件设计层次如下图:        在这个图中我们可以看到,Framework处于Micro-archite ...

  8. 初学JDBC,防SQL注入简单示例

    在JDBC简单封装的基础上实现 public class UserDao{ public static void testGetUser(String userName) throws Excepti ...

  9. SSH协议及其应用

    SSH协议及其应用 原文作者:阮一峰 链接: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruany ...

  10. vm虚拟机自定义安装centOS找不到网卡

    问题:自定义简化安装后执行ifconfig无法找到eth0网卡 1.查看eth0网络配置: [root@minion1 ~]# cat /etc/sysconfig/network-scripts/i ...