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 ...
随机推荐
- 设置多个ip ,实现ip欺骗
网关和DNS填写: 使用IP欺骗功能必须得本地有多个可用IP,通常普通的PC机只有一个物理网卡,这就需要我们手工设置多IP绑定同一网卡: a.开始菜单 -> 控制面板 -> ...
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- [UE4]关闭自动曝光
向光移动,屏幕会慢慢变亮:背光移动,屏幕会慢慢变暗. 关闭自动曝光: 编辑->项目设置->搜索Auto exposure
- Linux命令详解-文件系统管理
1. 外部设备简介 (1.)硬盘的分类: IDE硬盘 ./dev/hda hdb,hdc… 分区后:/dev/hda1 /dev/hda2 scsi硬盘: /dev/sda sdb,sdc ...
- Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本)
Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本) Hive 安装依赖 Hadoop 的集群,它是运行在 Hadoop 的基础上. 所以在安装 Hive 之 ...
- 用深度学习LSTM炒股:对冲基金案例分析
英伟达昨天一边发布“全球最大的GPU”,一边经历股价跳水20多美元,到今天发稿时间也没恢复过来.无数同学在后台问文摘菌,要不要抄一波底嘞? 今天用深度学习的序列模型预测股价已经取得了不错的效果,尤其是 ...
- 对抗样本攻防战,清华大学TSAIL团队再获CAAD攻防赛第一
最近,在全球安全领域的殿堂级盛会 DEF CON 2018 上,GeekPwn 拉斯维加斯站举行了 CAAD CTF 邀请赛,六支由国内外顶级 AI 学者与研究院组成的队伍共同探讨以对抗训练为攻防手段 ...
- web.py尝试性学习!
首先导入web.py模块! import web 没有的话就: pip install web web.py的URL结构: urls = ( '/', "index" ) 第一部分 ...
- 暴搜 - Codeforces Round #327 (Div. 2) E. Three States
E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...
- CUDA C Programming Guide 在线教程学习笔记 Part 13
▶ 纹理内存访问补充(见纹理内存博客 http://www.cnblogs.com/cuancuancuanhao/p/7809713.html) ▶ 计算能力 ● 不同计算能力的硬件对计算特性的支持 ...