QUESTION

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)

1st TRY

class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
minInitHP = INT_MAX;
dfs(dungeon,,,,);
return minInitHP+1;
}
void dfs(vector<vector<int> > &dungeon, int m, int n, int currentHP, int currentMinInitHP)
{
currentHP -= dungeon[m][n];
currentMinInitHP= max(currentMinInitHP,currentHP);
if(currentMinInitHP>minInitHP) return;
if(m==dungeon.size()- && n==dungeon[].size()-)
{
currentHP = min(currentHP,currentMinInitHP);
return;
}
if(m!=dungeon.size()-) dfs(dungeon,m+,n,currentHP,currentMinInitHP);
if(n!=dungeon[].size()-) dfs(dungeon,m,n+,currentHP,currentMinInitHP); }
private:
int minInitHP;
};

Result: Time Limit Exceeded

2nd TRY

用空间换时间,动态规划

class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
int m = dungeon.size();
int n = dungeon[].size(); int **dp = new int*[m]; // min HP needed when knight come here
for(int i=; i < m; i++)
dp[i] = new int[n]; dp[][] = - dungeon[][];for(int i = ; i < m; i++)
{
dp[i][] = max(dp[i-][]-dungeon[i][], dp[i-][]);
}
for(int i = ; i < n; i++)
{
dp[][i] = max(dp[][i-]-dungeon[][i], dp[][i-]);
}
for(int i = ; i < m; i++)
{
for(int j = ; j < n; j++)
{
dp[i][j]=min(dp[i-][j],dp[i][j-])-min(,dungeon[i][j]);
}
}
return max(,dp[m-][n-]+);
}
};

Result: Wrong Answer

Input: [[0,5],[-2,-3]]
Output: 4
Expected: 1

3rd TRY

状态的保存有问题,需要保存两个状态:到当前格在初始时需要的HP,以及到了当前格的HP

所以要从下往上填状态,那么只要保存一个状态,当前格的HP

class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
int m = dungeon.size();
int n = dungeon[].size(); int **dp = new int*[m]; // min HP needed when knight from here to end
for(int i=; i < m; i++)
dp[i] = new int[n]; dp[m-][n-] = max( - dungeon[m-][n-],);
for(int i = m-; i >= ; i--)
{
dp[i][n-] = max(dp[i+][n-]-dungeon[i][n-],);
}
for(int i = n-; i >= ; i--)
{
dp[m-][i] = max(dp[m-][i+]-dungeon[m-][i],);
}
for(int i = m-; i >= ; i--)
{
for(int j = n-; j >= ; j--)
{
dp[i][j]=max(min(dp[i+][j],dp[i][j+])-dungeon[i][j],);
}
}
return dp[][]+;
}
};

Result: Accepted

Dungeon Game (GRAPH - DP)的更多相关文章

  1. SCOJ 4427: Miss Zhao's Graph dp

    4427: Miss Zhao's Graph 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4427 Description Mr Jiang ...

  2. F. Clique in the Divisibility Graph DP

    http://codeforces.com/contest/566/problem/F F. Clique in the Divisibility Graph time limit per test ...

  3. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  4. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  5. Codeforces.566F.Clique in the Divisibility Graph(DP)

    题目链接 \(Description\) 给定集合\(S=\{a_1,a_2,\ldots,a_n\}\),集合中两点之间有边当且仅当\(a_i|a_j\)或\(a_j|a_i\). 求\(S\)最大 ...

  6. 63. Unique Paths II (Graph; DP)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 62. Unique Paths (Graph; DP)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. 64. Minimum Path Sum (Graph; DP)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. codeforces 459E E. Pashmak and Graph(dp+sort)

    题目链接: E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input st ...

随机推荐

  1. 以后可能会遇到的问题记录 .send_keys 无法输入的情况

    1.send_keys(输入不了字符)也出现这种问题,后来用set_value 可以了 send_keys():调用当前系统输入法键盘,进行输入时可能无法到达自己想  要的结果  set_text() ...

  2. PHP 弹出文件下载 原理 代码

    /** * @author      default7<default7@zbphp.com> * @description 演示PHP弹出下载的原理 * * @param $file_n ...

  3. javascript通过改变滚动条滚动来显示某些元素的scrollIntoView()方法

    scrollIntoView(b)可以在任何HTML上调用,通过滚动滚动条,调用的元素就可以出现在可视区域. 参数如果是true,或者不传参数,则表示调用元素的顶部与浏览器顶部平齐. 如果传入fals ...

  4. BTree,B-Tree,B+Tree,B*Tree的数据结构

    B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...

  5. collections模块---(namedtuple、deque、OrderdDict、defaultdict、Counter)和configparser模块

    在内置数据类型(dict. list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter. deque.defaultdict.namedtuple 和 ...

  6. selenium+python自动化96-执行jquery报:$ is not defined

    前言 背景介绍:做wap页面自动化的时候,把url地址直接输入到浏览器(chrome浏览器有手机wap模式)上测试,有个按钮死活点不到,用wap模式的触摸事件也无法解决,后来想用jquery去执行点击 ...

  7. selenium+python自动化92-多线程启动多个不同浏览器

    前言 如果想用多个浏览器跑同一套测试代码,driver=webdriver.Firefox()这里的driver就不能写死了,可以把浏览器名称参数化. 后续如果想实现多线程同时启动浏览器执行用例,用前 ...

  8. tornado-通过判断后台数据限制登陆--简单的

    import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # 提供 ...

  9. UVA-550

    题意 输入进制数n,第一个乘数的最后一位m,第二个乘数k,乘法的结果为mk, mk的第一位是m,求此时mk的长度 #include<iostream> #include <stdio ...

  10. tab form

    procedure TForm2.ToolButton1Click(Sender: TObject); var frm: TForm; begin frm := TForm.Create(Applic ...