174.Dungeon Game---dp
题目大意:从左上角到右下角,每一个格子都有各自的权值,如果权值为负,则当到达时,要失血;如果权值为正,则当到达时,要加血。当到达某个格子时,当前血量<=0,则死亡,到达不了右下角,所以此时要计算从左上角到右下角,初始应该最少携带多少血(即经过所有路径后所计算出的值),才不会死亡,能正常到达右下角。
法一:dfs,模板深搜,果断超时。要注意:更新点不是整条路径的和(与64题比较)的最小值,而是整条路径中所达到的最大的失血量,如果最大失血量>=0,则本身只需要携带1个血即可;否则本身携带的血应该=abs(最大失血量)+1。代码如下:
public int calculatedMinimumHP(int[][] dungeon) {
boolean vis[][] = new boolean[dungeon.length][dungeon[0].length];
int f[][] = {{1, 0}, {0, 1}};
vis[0][0] = false;
return dfs(dungeon, 0, 0, dungeon[0][0], Integer.MAX_VALUE, dungeon[0][0], vis, f);
}
public int dfs(int[][] dungeon, int x, int y, int sum, int res, int blood, boolean vis[][], int f[][]) {
if(x == dungeon.length - 1 && y == dungeon[0].length - 1) {
//递归结束点是每条线路的过程中的最大失血量
if(blood < 0) {//如果失血量为负数,则是绝对值+1
if(Math.abs(blood) < res) {
res = Math.abs(blood) + 1;
}
}
else {//如果失血量>=0,则是1,因为当失血量为0时,也会死亡
res = 1;
}
return res;
}
for(int i = 0; i < 2; i++) {
int cnt_x = x + f[i][0];
int cnt_y = y + f[i][1];
if(cnt_x < dungeon.length && cnt_y < dungeon[0].length && vis[cnt_x][cnt_y] == false) {
vis[cnt_x][cnt_y] = true;
res = dfs(dungeon, cnt_x, cnt_y, sum + dungeon[cnt_x][cnt_y], res, blood < (sum + dungeon[cnt_x][cnt_y]) ? blood : (sum + dungeon[cnt_x][cnt_y]), vis, f);
vis[cnt_x][cnt_y] = false;
}
}
return res;
}
法二(借鉴):dp,类似于64的二维dp,64是从左上往右下,而这题是从右下往左上。dp[i][j]表示从坐标[i,j]到右下角的路径中需要的最少血量,这样每次计算时,都可以用到下面和右面的dp值,从中取最小再将当前值减去即可。其中要追的地方与dfs相似,在每一个格子中,都要始终保持至少1个血量。dp公式:dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) - du[i][j]。代码如下(耗时2ms):
public int calculatedMinimumHP(int[][] dungeon) {
int dp[][] = new int[dungeon.length][dungeon[0].length];
//初始化最后一列
//由于走到每个格子时,都要保持至少一个血量,所以应该用到max(1, ...)
dp[dungeon.length - 1][dungeon[0].length - 1] = Math.max(1, 1 - dungeon[dungeon.length - 1][dungeon[0].length - 1]);
for(int i = dungeon.length - 2; i >= 0; i--) {
dp[i][dungeon[0].length - 1] = Math.max(1, dp[i + 1][dungeon[0].length - 1] - dungeon[i][dungeon[0].length - 1]);
}
//初始化最后一行
for(int i = dungeon[0].length - 2; i >= 0; i--) {
dp[dungeon.length - 1][i] = Math.max(1, dp[dungeon.length - 1][i + 1] - dungeon[dungeon.length - 1][i]);
}
//计算dp
for(int i = dungeon.length - 2; i >= 0; i--) {
for(int j = dungeon[0].length - 2; j >= 0; j--) {
//从下边和右边中取出最小者,然后减去当前值
dp[i][j] = Math.max(1, Math.min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]);
}
}
return dp[0][0];
}
174.Dungeon Game---dp的更多相关文章
- Java for 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 ...
- [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 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 174. Dungeon Game
题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...
- 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 174. Dungeon Game (C++)
题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...
- 174 Dungeon Game 地下城游戏
一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数 ...
随机推荐
- java网络编程(二)可中断套接字
参考资料:java核心技术 卷II 为中断套接字操作,可使用java.nio包提供的SocketChannel类.可以使用如下方式打开SocketChannel: SocketChannel chan ...
- 计算机网络:A、B、C、D和E类IP地址
IP地址分为A,B,C,D,E五类. 网络号:用于识别主机所在的网络:主机号:用于识别该网络中的主机. 其中A类分配给政府机关使用,B类地址给大中型企业使用,C类地址给个人使用.这三种是主要的. IP ...
- JVM堆内存控制/分代垃圾回收
JVM的堆的内存, 是通过下面面两个参数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后 ...
- JAVA里面"=="和euqals的区别
(1)基本数据类型,用双等号“==”比较,比较的是他们的值,值类型是存储在内存中的栈中 (2)复合数据类型中, 当他们用“==”进行比较的时候,比较的是他们在内存中的存放地址,其变量在栈中仅仅是存储引 ...
- Docker学习笔记三:Docker部署Java web系统
Docker部署Java Web系统 1.在root目录下创建一个路径test/app mkdir test && cd test&& mkdir app && ...
- 【WPF】日常笔记(持续更新)
本文专用于记录WPF开发中的小细节,作为备忘录使用. 1. 关于绑定: Text ="{Binding AnchorageValue,Mode=TwoWay,UpdateSourceTrig ...
- SQL通用优化方案(where优化、索引优化、分页优化、事务优化、临时表优化)
SQL通用优化方案:1. 使用参数化查询:防止SQL注入,预编译SQL命令提高效率2. 去掉不必要的查询和搜索字段:其实在项目的实际应用中,很多查询条件是可有可无的,能从源头上避免的多余功能尽量砍掉, ...
- 《javascript高级程序设计(第3版)》-1
javascript有下列三个不同的部分组成: ECMAScript,由ECMA-262定义,提供核心语言功能 文档对象模型(DOM),提供访问和操作网页内容的方法和接口 浏览器对象模型(BOM),提 ...
- HDU 5489 二分 LIS
Removed Interval Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 限制SSH远程登录用户仅能只读访问Linux中指定的目录
资料参考:http://os.51cto.com/art/201703/534895.htm 背景需求: 在TOMCAT服务器上建立一个普通帐号log_user,只能查看TOMCAT日志,不能删改任何 ...