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; }};
- leetcode 174. Dungeon Game 一个逆着推导计算的DP动态规划问题
- 174. Dungeon Game-动态规划
- 算法学习之动态规划(leetcode 174. Dungeon Game)
- Leetcode Dungeon Game(经典动态规划)
- Dungeon Game 动态规划
- 【动态规划】Dungeon Game
- Leetcode—174. Dungeon Game 倒序DP
- Leetcode.174 Dungeon Game |算法解析——动态规划
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- leetCode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- PopWindow封装
- luogu P2890 便宜的回文
- World怎么去掉 某一文本框中的 红色或蓝色下划线
- 7-32 哥尼斯堡的“七桥问题”(25 分)
- 计算机网络(哈尔滨工业大学Mooc)
- leetcode 174. Dungeon Game 一个逆着推导计算的DP动态规划问题
- Spring Boot 实现最简单的 Security
- 按钮+pager+Xlistview
- linux shell脚本截取字符串的八种方式
- LinuxGPIO驱动使用其实很简单
- MAC平台上eclipse2017安装代码反编译插件
- 【编译错误】error:stray'\243'in program
- F
- HttpClite的HttpGet和HttpPost