174. Dungeon Game
来源:互联网 发布:国网天津物资 大数据 编辑:程序博客网 时间:2024/06/05 06:12
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.
public int calculateMinimumHP(int[][] dungeon){m = dungeon.length;n = dungeon[0].length;dfs(0, 0, 0, dungeon, Integer.MAX_VALUE);return min;}private void dfs(int i, int j, int sum, int[][] dungeon, int curminsum){sum += dungeon[i][j];curminsum = Math.min(curminsum, sum);if (i == m - 1 && j == n - 1){if (curminsum >= 0)min = 1;else{min = Math.min(min, -curminsum + 1);}return;}if (i < m - 1)dfs(i + 1, j, sum, dungeon, curminsum);if (j < n - 1)dfs(i, j + 1, sum, dungeon, curminsum);}
解题思路
一看就是典型的动态规划问题,不过这道题从左上角开始递推不好推,因为要考虑两个因素,一是剩余的血越多越好(血量多可以使得以后的路程中血量更加高一点),行进过程中血量最低值越高越好(这个直接影响到结果),这两个因素都要考虑,且相互影响。其实我们可以从右下角开始推,dp表示如果从该格子出发,最少要多少初始血量。
public int calculateMinimumHP(int[][] dungeon){int m = dungeon.length;int n = dungeon[0].length;int[][] ans = new int[m][n];ans[m - 1][n - 1] = dungeon[m - 1][n - 1] > 0 ? 0: -dungeon[m - 1][n - 1];for (int i = m - 2; i >= 0; i--){ans[i][n - 1] = dungeon[i][n - 1] >= ans[i + 1][n - 1] ? 0: ans[i + 1][n - 1] - dungeon[i][n - 1];}for (int j = n - 2; j >= 0; j--){ans[m - 1][j] = dungeon[m - 1][j] >= ans[m - 1][j + 1] ? 0: ans[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 down = dungeon[i][j] >= ans[i + 1][j] ? 0 : ans[i + 1][j]- dungeon[i][j];int right = dungeon[i][j] >= ans[i][j + 1] ? 0 : ans[i][j + 1]- dungeon[i][j];ans[i][j] = Math.min(down, right);}return ans[0][0] + 1;}
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- 174. Dungeon Game
- leetCode 174. Dungeon Game
- 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- 机器视觉-行人检测
- Linux 系统应用编程——网络编程(TCP/IP 数据包格式解析)
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- java基础知识:封装
- 上班之前的“踩点”--一个程序员的成长史(9)
- 174. Dungeon Game
- 析构函数,在using结束时,会调用
- Arduino 卡片9感光灯
- Linux 系统应用编程——网络编程(利用TCP/IP 模型分析数据传输过程)
- iOS 友盟推送三种状态(app在当前界面,app在后台,app未启动)时调用方法
- 根据传入的数据绘制圆环,以及以绘画形式自定义图片为圆形
- L2-010. 排座位(并查集水题)
- 简单几招破解Windows管理员密码
- 【Ionic实战】一个和AngularJS的跨平台(iOS,Android) APP框架