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. [转]C# 系统应用之鼠标模拟技术及自动操作鼠标

    原文网址: C# 系统应用之鼠标模拟技术及自动操作鼠标        游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C# ...

  2. selenium java-2 chrome driver与对应版本

    chrome driver下载地址:https://npm.taobao.org/mirrors/chromedriver driver与chrome的对应关系: 1.进入最新的driver,查看no ...

  3. ubuntu 查看系统配置

    1, 主板信息 .查看主板的序列号 -------------------------------------------------- #使用 命令 dmidecode | grep -i 'ser ...

  4. java 总结代码块

    判断str2在str中出现了多少次: //msg: // // 世界上最痛苦的事 莫过于有眼睛却发现不了美 有耳朵却不会欣赏音乐 有心灵却无法理解什么是最真 // 世界上最痛苦的事 莫过于错过了不该错 ...

  5. Spark JDBC入门测试

    spark jdbc分支源码下载地址 https://github.com/apache/spark/tree/branch-1.0-jdbc 编译spark jdbc  ./make-distrib ...

  6. REST 风格

  7. python之路之函数03

    一 首先我们学到函数的嵌套调用与定义:1 函数嵌套 # def f1(): # print(f1)#我们这里如果输入f1那么输出的则是f1这个变量(函数)所在的地址.如果输入一个字符的话那么就直接输出 ...

  8. [Python] Hermite 插值

    # -*- coding: utf-8 -*- #Program 0.5 Hermite Interpolation import matplotlib.pyplot as plt import nu ...

  9. UVA-10115

    字符查找替换,WA了N次,一次只能替换一个,下一次find必须从第0个位置开始 import java.io.File; import java.io.FileNotFoundException; i ...

  10. 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv

    使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...