您的位置:首页 > 其它

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息