✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
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,但是如何用DP,刚开始的想法是,
1、用两个数组,一个数组记录到当前位置需要的最小血量min[len],另一个是在最小血量的情况下路径上的数的和sum[len]。
2、第一行因为只能一直向右走,所以很简单。
3、从第二行开始,有两种选项,从上面来还是从左边来(第一个单独算),然后比较这两种选项需要的最小血量,选择较小的那条路。
4、在两种选项的血量一样的时候,选择sum较大的走。
但是这样会出错。
[[1,-3,3],[0,-2,0],[-3,-3,-3]]这一组就会出错,因此这样是不对的。局部最优不等于全局最优。
public class Solution {//错误代码
public int calculateMinimumHP(int[][] dungeon) {
if (dungeon.length == 1 && dungeon[0].length == 1){
if (dungeon[0][0] > 0){
return 1;
} else {
return -dungeon[0][0]+1;
}
}
int len = dungeon[0].length;
int[] min = new int[len];
int[] sum = new int[len];
int num = dungeon[0][0];
min[0] = Math.max(1, -num + 1);
sum[0] = num;
for (int i = 1; i < len; i++){
num += dungeon[0][i];
sum[i] = num;
min[i] = Math.max(-num + 1, min[i - 1]);
}
int flag = 0;
int flag2 = 1;
for (int i = 1;i < dungeon.length; i++){
sum[0] += dungeon[i][0];
min[0] = Math.max(min[0], -sum[0] + 1);
for (int j = 1;j < len; j++){
if (min[j - 1] < min[j] || (min[j - 1] == min[j] && sum[j - 1] > sum[j])){
sum[j] = sum[j - 1] + dungeon[i][j];
min[j] = Math.max(min[j - 1], -sum[j] + 1);
} else {
sum[j] += dungeon[i][j];
min[j] = Math.max(min[j], -sum[j] + 1);
}
}
}
return min[len - 1];
}
}
2、反过来,从结尾开始,表示当前位置到结尾最少需要的血量
public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
if (dungeon.length == 1 && dungeon[0].length == 1){
if (dungeon[0][0] > 0){
return 1;
} else {
return -dungeon[0][0] + 1;
}
}
int len = dungeon[0].length;
int row = dungeon.length;
int[] min = new int[len];
min[len - 1] = -dungeon[row - 1][len - 1] + 1;
min[len - 1] = Math.max(min[len - 1], 1);
for (int i = len - 2; i >= 0; i--){
min[i] = min[i + 1] - dungeon[row - 1][i];
min[i] = Math.max(min[i], 1);
}
for (int i = row - 2; i >= 0; i--){
min[len - 1] = min[len - 1] - dungeon[i][len - 1];
min[len - 1] = Math.max(min[len - 1], 1);
for (int j = len - 2; j >= 0; j--){
min[j] = Math.min(min[j + 1], min[j]) - dungeon[i][j];
min[j] = Math.max(min[j], 1);
}
}
return min[0];
}
}
✡ leetcode 174. Dungeon Game 地牢游戏 --------- java的更多相关文章
- [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地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Java for LeetCode 174 Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 174 Dungeon Game 地下城游戏
一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数 ...
- leetcode@ [174] Dungeon Game (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
- Leetcode#174 Dungeon Game
原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...
- 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)最初被安置在左上角的房间里,他必须穿过地下 ...
随机推荐
- 提高开发效率的十五个Visual Studio 2010使用技巧
相信做开发的没有不重视效率的.开发C#,VB的都知道,我们很依赖VS,或者说,我们很感谢VS.能够对一个IDE产生依赖,说明这个IDE确实有它的独特之处.无容置疑,VS是一个非常强大的IDE,它支持多 ...
- Python开发入门与实战14-基于Extjs的界面
14. 基于Extjs的界面 上一章我们实现了一个原生的html例子,本章我们将采用Extjs实现界面的展现,来说明MVC模式下我们是怎么考虑界面与业务层的关系的. 14.1. 引用Extjs目录 首 ...
- Javascript闭包(狗血剧情,通俗易懂)
我们先来看一个闭包的函数: function a() { var i = 0; function b() { alert(++i); } return b; } var c = a(); c(); c ...
- js和jquery获取图片真实的宽度和高度
1.什么时候需要获取图片真实的宽度和高度 在做pc网页的时候,有时候会考虑按照插入的图片的尺寸来判断图片是横图还是竖图.然后判断过后给予不同的展示方式! 另外一种就是在手机页面上,在新闻页插入的图片往 ...
- Android数据存储-读取内部存储空间数据
内部存储空间的默认位置 data/data/应用名称 写数据,获取FileOutPutStream的方式 1.直接写死路径的方式 FileOutputStream fos = new FileOutp ...
- android性能测试与调优:使用 DDMS 查看内存分配情况
1. 启用自己的APK后 2. 点击左边更新heap 3. 点击右边的heap中的垃圾回收cause GC,等待数秒出现回收内存与数据情况(由于内存回收了APK运行出现异常crash) 4. 点击一个 ...
- KBEngine 学习笔记
最近开始学习 KBE 扩展技能点>_<!所以建一个随笔记录一下遇到的小问题: 问题1 :DBMgr找不到LibMysql32.dll 解决:VS 中KBE源码 默认的是Win32 ,Win ...
- Thinkphp_基础(2)URL模式
URL请求 ThinkPHP采用单一入口模式访问应用,对应用的所有请求都定向到应用的入口文件,系统会从URL参数中解析当前请求的模块.控制器和操作,下面是一个标准的URL访问格式: http://se ...
- Inversion_树状数组***
Problem Description You have a sequence {a1,a2,...,an} and you can delete a contiguous subsequence o ...
- POJ 2976
http://poj.org/problem?id=2976 01分数规划问题,可以舍掉k组 01分数规划用于解决的经典问题是最优比率生成树 解法见http://www.cnblogs.com/lot ...