【原创】leetCodeOj --- Dungeon Game 解题报告
原题地址:
https://oj.leetcode.com/problems/dungeon-game/
题目内容:
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.
方法:
唉,leetcode也越来越水了。这题是比较单纯的DP题,标Medium还行,标Hard有点让人失望。
步入正题。
定义状态dp[i][j]为,进入i,j坐标【前】,至少需要多少HP,才能从i,j坐标到达终点。
由于在一个坐标时,不是走右就是走下,所以:
设next = min(dp[i + 1][j] , dp[i][j + 1]) , pos = dungeon[i][j]
dp[i][j] = pos >= next ? 1 : next - pos
简单讲一下。首先next的值,是在i,j坐标上的right、down两个选择中,能够以最少HP到达终点的坐标的所需的最少HP值。(有点拗口。。比如说往右走需要进入右边以前有10点HP才能最终到达终点,而往下走则需要5点,那么next的值就是5)
其次,如果i,j坐标能够提供足够的HP保证走到next,那么进入i,j坐标前有1点HP就足够了,保证不死就行;反之,需要用next - pos来算出进入i,j坐标前需要的HP点数。
具体实现上,用一个一维dp数组就可以模拟整个过程了,不需要真的申请一个二维数组。具体可以参考代码,如果不能理解可以留言,我再详细讲讲。
具体代码:
class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
int ylength = dungeon.size(); // how many sub-array
if (ylength == 0)
return 0;
int xlength = dungeon[0].size(); // how many elements that sub-array contains
if (xlength == 0)
return 0;
int max = ~(1 << 31);
vector<int> res;
for (int i = 0; i < xlength; i ++) {
res.push_back(0);
}
for (int i = ylength - 1; i >= 0; i --) {
for (int j = xlength - 1; j >= 0; j --) {
int x = j + 1;
int y = i + 1;
int right = x < xlength ? res[x] : max;
int down = y < ylength ? res[j] : max;
if (right == max && down == max) {
res[j] = dungeon[ylength - 1][xlength - 1] >= 0 ? 1 : 1 - dungeon[ylength - 1][xlength - 1]; // final point
} else {
int tmp = right > down ? down : right;
int pos = dungeon[i][j];
if (pos >= tmp) {
res[j] = 1;
} else {
res[j] = tmp - pos;
}
}
}
}
return res[0];
} };
【原创】leetCodeOj --- Dungeon Game 解题报告的更多相关文章
- 【原创】leetCodeOj --- Min Stack 解题报告
题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...
- 【原创】leetCodeOj --- Largest Number 解题报告
原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- 【原创】leetCodeOj --- Sort List 解题报告
今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked lis ...
- 【原创】leetCodeOj ---Partition List 解题报告
原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...
- 【原创】leetCodeOj --- Interleaving String 解题报告
题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3 ...
- 【LeetCode】174. Dungeon Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【原创】ZOJ_1649 Rescue 解题报告
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- 【洛谷】NOIP2018原创模拟赛DAY1解题报告
点此进入比赛 T1:小凯的数字 题意:给定q个l,r,求l(l+1)(l+2)...(r-1)r模9的结果 很显然,这是道考验数(运)学(气)的题目 结论:输出\((l+r)*(r-l+1)\over ...
随机推荐
- hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes
今天学的hash.说实话还没怎么搞懂,明天有时间把知识点总结写了,今天就小小的写个结题报告吧! 题意: 在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角 ...
- Codeforces 474B Worms 二分法(水
主题链接:http://codeforces.com/contest/474/problem/B #include <iostream> #include <cmath> #i ...
- 《转》python 网络编程
原地址:http://blog.163.com/benben_long/blog/static/19945824320121225918434/ 网络客户端: 1. 理解socket: socket是 ...
- [SVN]常见问题的解决方案
Date:2014-1-3 Summary: SVN使用的一些常见问题解决方案记录,来源Internet,本人亲测 Contents: 1.回滚自己的分支到某一个版本 $svn merge -r rH ...
- android之JSON 进行网络数据交换
什么是JSON JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写,同一时候也易于机器解析和生成,很适合于server与client的交互. J ...
- VSTO之旅系列(四):创建Word解决方案
原文:VSTO之旅系列(四):创建Word解决方案 本专题概要 引言 Word对象模型 创建Word外接程序 小结 一.引言 在上一个专题中主要为大家介绍如何自定义我们的Excel 界面的,然而在这个 ...
- [品质生活] 舒适 Schick HYDRO 5剃须刀
[品质生活] 舒适 Schick HYDRO 5剃须刀 [品质生活] 舒适 Schick HYDRO 5剃须刀
- 使用javaDate类代数据仓库维度表
使用javaDate类代数据仓库维度表 Date类别: ,返回一个相对日期的毫秒数.精确到毫秒.但不支持日期的国际化和分时区显示. Date 类从Java 开发包(JDK)1.0 就開始进化,当时它仅 ...
- 添加PDF文件对照度的粗浅原理,及方法
上边这张照片不是异形,而是著名的鹦鹉螺.下边这张照片,是送给研究生同学的毕业纪念,向龙同学帮我激光雕刻的. 近期的照片在[http://www.douban.com/photos/album/13 ...
- JAVA insert() 插入字符串 reverse() 颠倒 delete()和deleteCharAt() 删除字符 replace() 替换 substring() 截取子串
insert() 插入字符串 StringBuffer insert(int index,String str) StringBuffer insert(int index,char ch) Stri ...