leetcode 174. Dungeon Game 一个逆着推导计算的DP动态规划问题

来源:互联网 发布:c语言编程汉诺塔 编辑:程序博客网 时间:2024/05/09 08:44

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,本来挺简单,但是怎么做不出来呢?这次要从结尾向后递推。

值得反思。

代码如下:

/* * 这个问题暂时想不清楚 * 但是是DP求解方法 * */public class Solution {    public int calculateMinimumHP(int[][] dungeon)     {        int m = dungeon.length;        int n = dungeon[0].length;        int[][] dp = new int[m][n];        dp[m-1][n-1] = minHP(1-dungeon[m-1][n-1]);        for(int i=m-2; i>=0; i--)             dp[i][n-1] = minHP( dp[i+1][n-1] - dungeon[i][n-1] );        for(int j=n-2; j>=0; j--)             dp[m-1][j] = minHP( dp[m-1][j+1] - dungeon[m-1][j]);        for(int i=m-2; i>=0; i--)         {            for(int j=n-2; j>=0; j--)                 dp[i][j] = minHP( Math.min(dp[i+1][j]- dungeon[i][j], dp[i][j+1]- dungeon[i][j]) );        }        return dp[0][0];    }    private int minHP(int x)     {        return x<=0? 1 : x;    }}

下面是C++的做法,这道题就是一个典型的动态规划问题

代码如下:

#include <iostream>#include <vector>#include <string>#include <map>#include <cmath>#include <algorithm>using namespace std;class Solution {public:    int calculateMinimumHP(vector<vector<int>>& d)     {        int row = d.size();        int col = d[0].size();        vector<vector<int>> dp(row,vector<int>(col,0));        dp[row-1][col-1] = minHP(1- d[row - 1][col - 1]);        for (int i = row - 2; i >= 0; i--)            dp[i][col-1] = minHP(dp[i+1][col - 1]- d[i][col - 1]);        for (int i = col - 2; i >= 0; i--)            dp[row - 1][i] = minHP(dp[row - 1][i + 1] - d[row - 1][i]);        for (int i = row - 2; i >= 0; i--)            for (int j = col - 2; j >= 0; j--)                dp[i][j] = minHP(min(dp[i + 1][j] - d[i][j], dp[i][j + 1] - d[i][j]));        return dp[0][0];    }    int minHP(int x)    {        return x <= 0 ? 1 : x;    }};
原创粉丝点击