LeetCode 174. Dungeon Game (C++)
题目:
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) |
Note:
- 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[ i ][ j ]需要的最少生命值。先来看一个特殊的情况,因为到达最后一个格子,加上dungeon[ m ][ n ]后要有1生命值。如果dungeon[ m ][ n ]是负数,骑士到达该位置时要扣去相应的生命值,且最少要1生命,如果dungeon[ m ][ n ]是正数,则只需要1生命就够了,因为到这个位置还可以加生命值,所以不难发现dp[ m ][ n ] = max(1 - dungeon[ m ][ n ],1)。下面再来看通常情况,dp[ i ][ j ]的值实际上是由dp[ i+1 ][ j ]和dp[ i ][ j+1 ]来决定的,也就是骑士右面和下面哪个需要的生命值越少,则骑士会选择那条较少的路线。所以动态转移方程dp[ i ][ j ] = max(min(dp[ i+1 ][ j ],dp[ i ][ j+1 ]) - dungeon[ m ][ n ],1)。
为了方便计算我们可以扩充一行一列,便于计算边界值。
| -2 (K) | -3 | 3 |
| -5 | -10 | 1 |
| 10 | 30 | -5 (P) |
| 7 | 5 | 2 | INT_MAX |
| 6 | 11 | 5 | INT_MAX |
| 1 | 1 | 6 | 1 |
| INT_MAX | INT_MAX | 1 | INT_MAX |
程序:
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size();
int n = dungeon[].size();
vector<vector<int>> res(m+, vector<int>(n+,INT_MAX));
res[m][n-] = res[m-][n] = ;
for(int i = m-; i >= ; --i){
for(int j = n-; j >= ; --j){
res[i][j] = max(min(res[i+][j], res[i][j+])-dungeon[i][j], );
}
}
return res[][];
}
};
LeetCode 174. Dungeon Game (C++)的更多相关文章
- [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
原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...
- [leetcode]174. Dungeon Game地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 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 ...
随机推荐
- hdfs的java接口简单示例
public class HDFSDemo { private FileSystem fs = null; @Before public void init() throws IOException, ...
- python -- 解决UnicodeEncodeError问题
使用中文字段时,经常会出现该异常:UnicodeEncodeError: 'ascii' codec can't encode characters in position 解决方法1: 在开头加上 ...
- Mysql 安全登陆工具 mysql_config_editor
mysql_config_editor 帮助信息请查看 man mysql_config_editor 或 mysql_config_editor -? 或 mysql_config_editor s ...
- Linux下onvif客户端获取ipc摄像头 GetServices:获取媒体地址(有的h265摄像头必须要这个接口)
GetServices:获取媒体地址(有些h265的摄像头必须用到这个接口,得到获取能力时没获取到的另一个媒体地址) 鉴权:但是在使用这个接口之前是需要鉴权的.ONVIF协议规定,部分接口需要鉴权,部 ...
- day 88 Vue学习之八geetest滑动验证
本节目录 一 geetest前端web中使用 二 xxx 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 geetest前端web中使用 下载gt文件,官网地址,下面我 ...
- python 3.x 实现简单用户登录
import os import sys import getpass login_username = 'admin' login_password = ' u = 0 while u < 3 ...
- C语言学习笔记—code:blocks工具debug调试异常
C语言开发工具:code:bolocks17.12版本踩坑记录: 一.下载codeblocks后,安装完成,创建第一个项目在构建mian主函数时构建报错: Goto "Settings-&g ...
- Java序列化与反序列化(实践)
Java序列化与反序列化(实践) 基本概念:序列化是将对象状态转换为可保持或传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象.这两个过程结合起来,可以轻松地存储和传输数据. 昨天在一本书上 ...
- 安装OpenLDAP步骤
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackgao@gmail. ...
- c++ 文件位置相关操作
教学内容: l 文件定位操作 l fgetpos定位 l fsetpos设定位置 l 文件结束判断函数feof 一.文件定位操作 在C语言标准库里 获取文件位置的函数有ftell和fge ...