Leetcode.174 Dungeon Game |算法解析——动态规划
2016-09-06 19:54
239 查看
原题:
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.
=======================================
题目分析
该题目大意是让一个骑士去地牢拯救骑士掉血,有一些房间可以回血。并且在这题中骑士只能向下和向右走这个题目就是要找到从起点(左上角)到公主(右下角)之间最小代价路径。算法分析
我一开始做也没有多想,直接写了一个广搜的算法,但是在一些数量很大的时候就超时了,因为复杂度为O(2^n),然后我想了一下这应该是要用动态规划来优化。但是我又想少了,我是从起点开始使用动态规划,但是这样有一个问题,就是这样选择的路径是当前最优而不是全局最优,也就是有点贪心算法,所以导致有一些数据过不了,比如:然后就想动态规划要反过来用,从终点开始计算所需要的生命值。
关键的函数就是
minHP[i][j] = dungeon[i][j] >= min(minHP[i+1][j],minHP[i][j+1]?
0 : min(minHP[i+1][j],minHP[i][j+1] - dungeon[i][j]
稍微解释一下, minHP[i+1][j] 是从下边来,minHP[i][j+1] 是从右边来,如果所需要的minHP小于i,j当前房间的值,则说明在通过这间房间后到终点之间不需要额外的生命值,否则minHP-当前房间则证明通过该房间到终点还需要的额外生命值,如果当前房间是怪物的话则需要更多的生命值,否则则需要更少的生命值。
代码
#include<iostream> #include<vector> using namespace std; int calculateMinimumHP(vector<vector<int> >& dungeon) { int M = dungeon.size(); int N = dungeon[0].size(); vector<vector<int> > minHP(M,vector<int> (N)); if(dungeon[M-1][N-1]>=0) minHP[M-1][N-1] = 0; else minHP[M-1][N-1] = -dungeon[M-1][N-1]; for(int i = M-2 ; i>=0 ; i--) //对 第 N 列 进行处理 { minHP[i][N-1] = dungeon[i][N-1]>=minHP[i+1][N-1]? 0 : minHP[i+1][N-1] - dungeon[i][N-1]; } for(int j = N-2 ; j>= 0 ; j--) // 对 第 M 行 进行处理 { minHP[M-1][j] = dungeon[M-1][j]>=minHP[M-1][j+1]? 0 : minHP[M-1][j+1] - dungeon[M-1][j]; } for(int i = M-2 ; i>= 0 ; i--) for(int j = N-2 ; j >=0 ; j--) { int minHP_from_bot = dungeon[i][j]>=minHP[i+1][j]? 0 : minHP[i+1][j] - dungeon[i][j];; int minHP_from_right = dungeon[i][j]>=minHP[i][j+1]? 0 : minHP[i][j+1] - dungeon[i][j]; minHP[i][j]= min(minHP_from_bot,minHP_from_right); } re turn minHP[0][0]+1; }
做完这一题后leetcode给我推荐了另外一道动态规划的题,相对这以及我的解答。https://leetcode.com/problems/unique-paths/
int uniquePaths(int m, int n) { vector<vector<int> > V(m,vector<int>(n)); V[0][0] =1; for(int i = 1; i<m ;i++) 4000 V[i][0] = 1; for(int j = 1; j<n ;j++) V[0][j] = 1; for(int i = 1 ; i<m ;i++) for(int j = 1 ; j<n ;j++) { V[i][j] = V[i-1][j]+ V[i][j-1]; } return V[m-1][n-1]; }
相关文章推荐
- 【算法】找零钱-动态规划实现过程解析
- 【算法导论】贪心算法,动态规划 源码解析
- 算法分析及实例解析(二)——贪心算法、动态规划
- 图像缩放的双线性内插值算法的原理解析
- 视频中对多目标运动物体跟踪的算法解析
- 图4-3.类型解析算法 示意图
- [导入]MethodTable内存空间分配中加法运算算法解析
- 算法——动态规划
- 0-1背包算法(动态规划)
- 用动态规划解--滑雪题 算法分析
- 数据结构知识——树的三种不同遍历算法解析
- 图像缩放的双线性内插值算法的原理解析
- 动态规划 --压缩算法
- Linux源码中的mktime算法解析
- 阿拉伯数字转换英文数字表示算法解析及其实现
- 典型算法与ACM题目解析(3)—记忆化搜索的应用
- 矩阵链相乘算法(动态规划)--算法导论示例 - 付之东流 - CSDNBlog
- 利用后缀数组求字符串的最长重复子串的算法 利用二维数组求两个字符串的最长公共字串(动态规划)
- 一道算法题:(背包,贪心,动态规划)