Leetcode#174 Dungeon Game
典型的地图寻路问题
如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了。
如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如果当前位置能够提供的体力比向下走所需要的最小体力还多,那么当前位置只需要1的体力就够了;否则,计算出额外需要多少体力。
如果向右走,同理。
设任意坐标(i, j)处最少需要health[i][j]的体力才能通关,则有如下递推公式:
health[i][j] = min{
dungeon[i][j] >= health[i+1][j] ? 1 : health[i+1][j] - dungeon[i][j],
dungeon[i][j] >= health[i][j+1] ? 1 : health[i][j+1] - dungeon[i][j]
}
因为递推公式只用到了相邻层,所以可以在编码时压缩成1维数组节省空间。
代码:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
if (dungeon.empty() || dungeon[].empty()) return -;
int m = dungeon.size();
int n = dungeon[].size();
vector<int> health(n, );
health[n - ] = dungeon[m - ][n - ] >= ? : - dungeon[m - ][n - ];
for (int j = n - ; j >= ; j--)
health[j] = dungeon[m - ][j] >= health[j + ] ? : health[j + ] - dungeon[m - ][j];
for (int i = m - ; i >= ; i--) {
health[n - ] = dungeon[i][n - ] >= health[n - ] ? : health[n - ] - dungeon[i][n - ];
for (int j = n - ; j >= ; j--) {
int right = dungeon[i][j] >= health[j + ] ? : health[j + ] - dungeon[i][j];
int down = dungeon[i][j] >= health[j] ? : health[j] - dungeon[i][j];
health[j] = min(right, down);
}
}
return health[];
}
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 (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
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. ...
- [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)最初被安置在左上角的房间里,他必须穿过地下 ...
- 【LeetCode】174. Dungeon Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】174. Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
随机推荐
- 三种找回 linux root密码
找回 linux root密码的三种方法 第1种方法: 1.在系统进入单用户状态,直接用passwd root去更改2.用安装光盘引导系统,进行linux rescue状态,将原来/分区挂接上来,作法 ...
- Google账户无法登陆-Solved
Author:KillerLegend Date:2014.5.19 From:http://www.cnblogs.com/killerlegend/p/3737888.html 这几天不知道怎么回 ...
- Laravel 5 基础(十一)- 子视图和表单复用
我们需要处理编辑文章的问题.当然我们可以手工添加新的路由,就像这样: Route::get('/articles/{id}/edit', 'ArticleController@edit'); 让我们在 ...
- PHP 把GBK编码转换为UTF8
//把GBK编码转换为UTF8 $name="勿以善小而不为"; $name=iconv("GBK", "UTF-8", $name);
- opensuse 安装 Anaconda3 之后出现Could not start d-bus. Can you call qdbus?
最近在安装了opensue Leap42.1之后,想要学习一下python,就安装了Anaconda3,并且将Anaconda3的安装路径添加到了PATH里,但是在重新启动系统后,出现了"C ...
- MongoDB 学习笔记(二)—— MongoDB Shell
MongoDB自带一个JavaScript shell 可以从命令行中与MongoDB交互,功能非常强大.如在上一节最后一张图所看到,可以执行JavaScript程序. 运行Shell 前提是启动Mo ...
- 几条sql语句
1.行.列转换 --行转列 ),科目 ),分数 int) ) ) ) ) ) ) ) ) ) --方法1 select 姓名, end) as 语文, end) as 数学, end) as 物理 f ...
- NOJ1012-进制转换
进制转换 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 2214 测试通过 : 645 ...
- 【转】Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败
错误原因如下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot ...
- Why string is immutable in Java ?
This is an old yet still popular question. There are multiple reasons that String is designed to be ...