Dungeon Game (GRAPH - DP)
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)的更多相关文章
- 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 ...
- F. Clique in the Divisibility Graph DP
http://codeforces.com/contest/566/problem/F F. Clique in the Divisibility Graph time limit per test ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- 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\)最大 ...
- 63. Unique Paths II (Graph; DP)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- [UE4]单映射:TMap容器,字典表
一.TMap是什么 TMap是UE4中的一种关联容器,每个键都关联着一个值,形成了单映射关系.因此你可以通过键名来快速查找到值.此外,单映射要求每个键都是唯一的. 二.创建和填充单映射 如果你想创建一 ...
- 特征选择:方差选择法、卡方检验、互信息法、递归特征消除、L1范数、树模型
转载:https://www.cnblogs.com/jasonfreak/p/5448385.html 特征选择主要从两个方面入手: 特征是否发散:特征发散说明特征的方差大,能够根据取值的差异化度量 ...
- 管理oracle的一些知识
管理一个oralce软件: 如何管理数据库,须知道什么知识. 1.安装:位置,字符集 2.建库:什么数据库名称 3.数据库启动: nomout:读参数文件,一些初始化设置信息 mount:读取控制文件 ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #16 OOM Killer的运行与结构
HACK #16 OOM Killer的运行与结构(1) 本节介绍OOM Killer的运行与结构. Linux中的Out Of Memory(OOM) Killer功能作为确保内存的最终手段,可以在 ...
- setTimeout 方法带参数传递
setTimeout(callback, after, arg1, arg2); 其中,callback即function(){},after为时间参数,指多久后执行callback,单位为毫秒,30 ...
- Python - Django - ORM 操作表
ORM 的对应关系: 类 ---> 数据库表对象 ---> 数据库行属性 ---> 字段 操作数据库表 ---> ...
- uva-193-图染色-枚举
题意:n个节点,可用描成黑色或者白色,黑节点和黑节点不能相连,问最多描出多少黑节点 #include <iostream> #include <stdio.h> #includ ...
- 文件查找find命令
find命令总结: 常用选项: -name 查找/etc目录下以conf结尾的文件 find /etc -name '*conf' -iname 查找当前目录下文件名为aa的文件,不区分大小写 fin ...
- WINdows常用监控相关
参考网址: http://www.jb51.net/article/49986.htm 一.图新Shell下: 1. 最直观的:(在运行里面输入CMD,以下命令都是在CMD下输入的:) 输入 s ...
- XE Styles不见了
C:\Users\Public\Documents\Embarcadero\Studio\15.0\Styles 我移动到D盘了 D:\Users\Public\Documents\Embarcade ...