LeetCode174:Dungeon Game
来源:互联网 发布:python post上传图片 编辑:程序博客网 时间:2024/06/15 14:13
【题目】
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
.
Notes:
- The knight's health has no upper bound.
- Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
【分析】
容易得到动态规划公式:dp[i][j] = max{1,min{dp[i+1][j],dp[i][j+1]}-dungeon[i][j]}.其中dp[i][j]指的是在点i,j出发去最下角,需要的最小血量。
#include <iostream>#include <vector>using namespace std;class Solution {public: int Max(int a, int b) { if(a > b) return a; else return b; } int Min(int a, int b) { if(a > b) return b; else return a; } int calculateMinimumHP(vector<vector<int> > &dungeon) { int M = dungeon.size(); int N = dungeon[0].size(); vector<vector<int> > dp(M); for(int i = 0;i<M;i++) dp[i].resize(N); dp[M-1][N-1] = Max(1 - dungeon[M-1][N-1], 1); //初始化边界值 for(int i = M-2;i>=0;i--) dp[i][N-1] = Max(dp[i+1][N-1] - dungeon[i][N-1], 1); for(int j = N-2;j>=0;j--) dp[M-1][j] = Max(dp[M-1][j+1] - dungeon[M-1][j], 1); for(int i = M-2;i>=0;i--) for(int j = N-2;j>=0;j--) dp[i][j] = Max(Min(dp[i][j+1], dp[i+1][j]) - dungeon[i][j], 1); return dp[0][0]; }};int main(){ Solution s; vector<vector<int> > vv; vv={{19,14,-25,-20,-36},{-46,-72,-74,25,-24},{-38,-57,-38,-73,-23},{-12,1,-70,44,-98}}; //应该是115,确是64 cout << s.calculateMinimumHP(vv) << endl; return 0;}注意vector的使用,它可以当做动态的二维数组,来记录状态值。
- LeetCode174:Dungeon Game
- Leetcode174-Dungeon Game
- LeetCode174—Dungeon Game
- LeetCode174. Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- 马年英语总结
- 5.创建表,使用alter进行表信息的增删改,Oracle回收站,集合运算
- C编译器剖析_3.1 语法分析_C语言的表达式(1)
- Cloudera Manager 使用笔记---远程访问不了Hue的管理页面
- HD-CNN: HIERARCHICAL DEEP CONVOLUTIONAL NEURAL NETWORK FOR IMAGE CLASSIFICATION(泛读)
- LeetCode174:Dungeon Game
- 分糖果
- boost库学习之 scoped_ptr scoped_array
- Java_IO流_字符流与字节流
- mysql中多个表 master salve同步搭建过程
- 如何使用为逻辑运算实现位向量(PROGRAMMING PEARLS)
- 力所能及之struts2环境搭建
- 图像基础知识
- softirq/tasklet/workqueue的区别