原题地址:

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 解题报告的更多相关文章

  1. 【原创】leetCodeOj --- Min Stack 解题报告

    题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...

  2. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

  3. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  4. 【原创】leetCodeOj --- Sort List 解题报告

    今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked lis ...

  5. 【原创】leetCodeOj ---Partition List 解题报告

    原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...

  6. 【原创】leetCodeOj --- Interleaving String 解题报告

    题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3  ...

  7. 【LeetCode】174. Dungeon Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. 【原创】ZOJ_1649 Rescue 解题报告

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  9. 【洛谷】NOIP2018原创模拟赛DAY1解题报告

    点此进入比赛 T1:小凯的数字 题意:给定q个l,r,求l(l+1)(l+2)...(r-1)r模9的结果 很显然,这是道考验数(运)学(气)的题目 结论:输出\((l+r)*(r-l+1)\over ...

随机推荐

  1. fck编辑器的使用

    FCK编辑器的使用 注意:编辑器有浏览器缓存,所以修改配置后,一定要删一下缓存 这个编辑器是采用 html+javascript 开发出来的 通常作为插件来使用: 1,下载插件包 2,解压,加压之后看 ...

  2. MQ、JMS以及ActiveMQ

    MQ简介: MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们.消息传 ...

  3. [Cocos2d-x]节点的尺寸大小

    作为一个CCNode,本身没有大小而言,但是AddChild之后,便有了尺寸的概念. Cocos2d-x中对于一个节点的尺寸可以通过以下三个方法获取: CCSprite: getContentSize ...

  4. .idata数据的解析

    每类Section代表不同的数据,不同的数据存储组织方式一定是有非常大区别的.代码段与资源段一定区别巨大,这意味着我需要一个一个的学习每个段的解析. idata段解析 这个段主要存储的是导入符号信息. ...

  5. Android 结合实例学会AsyncTask的使用方法

    AsyncTask运行时经过四个步骤,运行四个方法:           1.onPreExecute(),执行在UI线程,能够设置或改动UI控件,如显示一个进度条           2.doInB ...

  6. [linux]chown和chmod命令

    chown chown命令是将指定文件的拥有者改为指定的用户或组 例如: chown mail:mail test.log,把test文件指定拥有者和组都为mail chown -R mail:mai ...

  7. OpenCV-Python教程(5、初级滤波内容)

    本篇文章介绍如何用OpenCV-Python来实现初级滤波功能. 提示: 转载请详细注明原作者及出处,谢谢! 本文介绍使用OpenCV-Python实现基本的滤波处理 本文不介绍滤波处理的详细概念,所 ...

  8. .net设计模式 - 单例模式

    DoNet设计模式实例之单例模式( Singleton Pattern) 一 : 单例模式的简介:(Brief Introduction) 单例模式(Singleton Pattern),保证一个类只 ...

  9. jquery ui中 accordion的问题及我的解决方法

    原文:jquery ui中 accordion的问题及我的解决方法 jquery有一套所谓的ui组件,很不错的.如果有兴趣的朋友,可以参考http://jqueryui.com/ 但其中的accord ...

  10. hdu1876(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1876 题意:问机器人到达终点的过程中最多有几次完全消耗完能量,消耗完这么多次能量的方式有几种. 分析: ...