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. shell 1基础

    shell简介 shell是一个用C语言编写的程序,是用户使用Linux的桥梁.shell既是一种命令语言,又是一种程序设计语言. shell脚本(shell script),是一种为shell编写的 ...

  3. [UE4]C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例

    相关内容: C++实现动态加载的问题:LoadClass<T>()和LoadObject<T>() http://aigo.iteye.com/blog/2281558C++静 ...

  4. 最小化Linux系统安装

    安装CentOS 5.9 基于vbox虚拟机,虚拟机内存1 G,虚拟硬盘大小8 G 虚拟网卡使用host only方式 创建卷组centos 独立的boot分区 home, root和swap分区皆是 ...

  5. CCKS 2018 | 最佳论文:南京大学提出DSKG,将多层RNN用于知识图谱补全

    作者:Lingbing Guo.Qingheng Zhang.Weiyi Ge.Wei Hu.Yuzhong Qu 2018 年 8 月 14-17 日,主题为「知识计算与语言理解」的 2018 全国 ...

  6. web.py尝试性学习!

    首先导入web.py模块! import web 没有的话就: pip install web web.py的URL结构: urls = ( '/', "index" ) 第一部分 ...

  7. Wndows 下npm 安装依赖时出现错误:MSBUILD : error MSB4132: The tools version "2.0" is unrecognized. Available tools versions are "4.0".

    当在Windows环境中使用npm install或者yarn 安装依赖时,可能会出现如下类似的错误: MSBUILD : error MSB4132: The tools version " ...

  8. python之socket编写

    Socket 类型 套接字格式: socket(family,type[,protocal]) 使用给定的地址族.套接字类型.协议编号(默认为0)来创建套接字. socket类型 描述 socket. ...

  9. 一,Android Studio笔记

    转自:https://developer.android.com/studio/intro/index.html 一.界面 Android Studio 主窗口由图 3 标注的几个逻辑区域组成. 工具 ...

  10. corejava-内容梳理