Java for LeetCode 174 Dungeon Game
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的更多相关文章
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- [LeetCode] 174. Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- leetcode@ [174] Dungeon Game (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
- Java实现 LeetCode 174 地下城游戏
174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来 ...
- Leetcode#174 Dungeon Game
原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...
- [leetcode]174. Dungeon Game地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- LeetCode 174. Dungeon Game (C++)
题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...
- leetcode 174. 地下城游戏 解题报告
leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...
- 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 ...
随机推荐
- angularjs的页面拆分思想
//app.js angular.module('MyModule', ['SubModule1', 'SubModule2']) .module('SubModule1', ['CommonModu ...
- Linux File、File Directory IO Operation Summary(undone)
目录 . 引言 . Linux下文件操作API . Linux下文件目录操作API . Linux下的其他设备操作API 1. 引言 Linux支持多种文件系统,如ext.ext2.minix.iso ...
- DNS安全浅议、域名A记录(ANAME),MX记录,CNAME记录
相关学习资料 http://baike.baidu.com/link?url=77B3BYIuVsB3MpK1nOQXI-JbS-AP5MvREzSnnedU7F9_G8l_Kvbkt_O2gKqFw ...
- Java JDBC下执行SQL的不同方式、参数化预编译防御
相关学习资料 http://zh.wikipedia.org/wiki/Java数据库连接 http://lavasoft.blog.51cto.com/62575/20588 http://blog ...
- 用Filter程序实现静态HTML页面的访问保护
今天为练习Filter的用法编写了一个小程序. 当用户通过article的超链接读取文章的时候,会通过Filter进行检测有没有登录.只有登录的读者才能跳到文章页面,否则跳到登录页面. 文章就用简单的 ...
- jQuery插件开发教程
jQuery插件开发教程 ——让你的jQuery水平提升一个台阶 要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台 ...
- WinForm TextBox 焦点停留到文本最后
最近写个 WinForm 项目,TextBox 控件有内容的时候,获取焦点,光标总是在最前面,很不便于输入.那怎么样让光标停留到最后呢?如下代码可以实现: this.txtBox ...
- Pb (数据存储单位)
PB (数据存储单位) 编辑 pb指petabyte,它是较高级的存储单位,其上还有EB,ZB,YB等单位. 它等于1,125,899,906,842,624(2的50次方)字节,“大约”是一千个te ...
- WebRequest中的工厂方法模式
- cpu和内存的关系
CPU是负责运算和处理的,内存是交换数据的.当程序或者操作者对CPU发出指令,这些指令和数据暂存在内存里,在CPU空闲时传送给CPU,CPU处理后把结果输出到输出设备上,输出设备就是显示器,打印机等. ...