Dungeon Game -- latched
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.
基本思路:
动态规划
设health[i][j] 为走进dungeon[i][j]的初始血量,且该个血量将能维持到骑士足以走完右下角。
已知条件:骑士走完右下角至少要剩一滴血。即health[m][n-1] = 1。
m为dungeon行数。
也能够设health[m-1][n] = 1。
此值表示走完右下角的剩余血量。
同一时候也是从该右下角向右,或者向下。走到还有一格时的初始血量。 当然此两格是虚拟的,地牢中不存在。或者形象的说,从右下角向右或者向下走出地牢后,剩余的血量。
从此点。能够倒推出health[0][0]。
骑士仅仅能向右,向下右移动。 要知道当前位置的初始血量,仅仅须要知道其右和其下的初始血量,就能够反推出。
即
health[i][j] = min(health[i+1][j], health[i[j+1]) - dungeon[i][j]
因为骑士要时刻保持血量至少为1. 上面能够改为:
health[i][j] = max(1, min(health[i+1][j], health[i[j+1]) - dungeon[i][j])
因为递推时仅仅须要其右和其下,两个位置, 能够使用滚动数组。用一维替换掉二维数组。
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
if (dungeon.empty() || dungeon[0].empty())
return 0;
const int m = dungeon.size();
const int n = dungeon[0].size();
vector<int> health(n+1, INT_MAX);
health[n-1] = 1;
for (int i=m-1; i>=0; i--) {
for (int j=n-1; j>=0; j--) {
health[j] = max(1, min(health[j], health[j+1]) - dungeon[i][j]);
}
}
return health[0];
}
};
Dungeon Game -- latched的更多相关文章
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- leetcode174. Dungeon Game
// learn from https://discuss.leetcode.com/topic/6912/c-dp-solution ''' class Solution { public: int ...
- 【leetcode】Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- 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. ...
- LeetCode Dungeon Game
原题链接在这里:https://leetcode.com/problems/dungeon-game/ 这是一道DP题,保存当前格到右下格所需要的最小体力,m*n的dp数组保存. 更新是Math.mi ...
随机推荐
- NX自动出图 发布啦
经过4个月的努力,终于面世啦!!!!1.安装文件 :http://yunpan.cn/Q49TWSJmy2i5Z 请下载后,按照“安装说明.txt ”进行安装!2.学习视频:http://yun ...
- windows server 2008 如何查看异常重启日志
下面蓝队网络为大家介绍下windows server 2008 如何查看异常重启日志 开始->管理工具->时间查看器 windows日志->系统 筛选当前日志 选择Kernel-Po ...
- ajax的底层前后台交互
为什么用ajax或者它的优点: 异步加载数据,无需切换页面 更加的用户体验,局部刷新,及时验证,操作步骤简化: 节省流量 js控制数据的加载,更加灵活多用. 底层就是XMLHttpRequest对象: ...
- Windows Socket五种I/O模型——代码全攻略(转)
Winsock 的I/O操作: 1. 两种I/O模式 阻塞模式:执行I/O操作完成前会一直进行等待,不会将控制权交给程序.套接字 默认为阻塞模式.可以通过多线程技术进行处理. 非阻塞模式:执行I/O操 ...
- 防止按钮button重复提交,点击后失效,10秒后恢复
<script type="text/javascript"> $(function () {//页面完全加载完后执行 /*防止重复提交 10秒后恢复*/ var is ...
- ie7下设置z-index无效如何解决?
ie7下z-index无效的问题之前做练习的时候遇到过,百度解决掉之后就丢脑后了.今天项目中又发现这个bug,无奈又去百度,这次还是记下来,节省了百度的时间还能小装一把... 需求是这样的: 页面中的 ...
- LeetCode141LinkedListCycle和142LinkedListCycleII
141题:判断链表是不是存在环! // 不能使用额外的存储空间 public boolean hasCycle(ListNode head) { // 如果存在环的 两个指针用不一样的速度 会相遇 L ...
- 面试总结——Java高级工程师(一)
一.无笔试题 不知道是不是职位原因还是没遇到,面试时,都不需要做笔试题,而是填张个人信息表格,或者直接面试 二.三大框架方面问题 1.Spring 事务的隔离性,并说说每个隔离性的区别 解答:spri ...
- 52.基于doc value正排索引的聚合内部原理
主要知识点: 本节没有太懂,以后复习时补上 聚合分析的内部原理是什么????aggs,term,metric avg max,执行一个聚合操作的时候,内部原理是怎样的呢?用了什么样的数据结 ...
- vue SSR 部署详解
先用vue cli初始化一个项目吧. 输入命令行开始创建项目: vue create my-vue-ssr 记得不要选PWA,不知为何加了这个玩意儿就报错. 后续选router模式记得选 histor ...