leetcode 174. Dungeon Game
来源:互联网 发布:软件测试员培训 编辑:程序博客网 时间:2024/06/06 17:41
174. Dungeon Game
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.
dungeon[i][j]代表到达i,j这个格子之前的最少血量,才能保证到达(i,j)之后不会死。
class Solution {public: int calculateMinimumHP(vector<vector<int>>& dungeon) { int row = dungeon.size(); if (row == 0) return 1; int col = dungeon[0].size(); //处理最后一排 dungeon[row-1][col-1] = (dungeon[row-1][col-1]<0) ? (1-dungeon[row-1][col-1]) : 1; for(int i = col - 2; i >= 0; i--) //处理最后一行 { if (dungeon[row-1][i] < dungeon[row-1][i+1]) dungeon[row-1][i] = dungeon[row-1][i+1] - dungeon[row-1][i]; else dungeon[row-1][i]=1; } for(int i = row - 2; i >= 0; i--) //处理最后一列 { if(dungeon[i][col-1] < dungeon[i+1][col-1]) dungeon[i][col-1] = dungeon[i+1][col-1] - dungeon[i][col-1]; else dungeon[i][col-1] = 1; } for(int i = row - 2; i >= 0; i--) { for(int j = col - 2; j >= 0; j--) { if (dungeon[i][j] < dungeon[i+1][j] && dungeon[i][j] < dungeon[i][j+1]) dungeon[i][j] = min(dungeon[i+1][j], dungeon[i][j+1]) - dungeon[i][j]; else dungeon[i][j] = 1; } } return dungeon[0][0]; }};
- [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
- Leetcode 174. Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- 174. Dungeon Game Leetcode Python
- [Leetcode]174. Dungeon Game @python
- [LeetCode]problem 174. Dungeon Game
- LeetCode | 174. Dungeon Game (Hard)
- 王垠:如何掌握所有的程序语言
- 所谓的算法,有时候几十行代码就能搞定!
- 模板整理: 部分数据结构
- Ubuntu打开某个软件,侧边栏显示打开,屏幕上不显示
- APP首次启动提示开启定位服务
- leetcode 174. Dungeon Game
- tenserflow中board可视化问题
- PHP实现搜索附近的人
- Aliddns插件使用:小白超详细图文教程
- 干货|Linux中限制目录占用空间
- 操作系统 实验一 多线程 (模拟)
- 常用惯性元器件
- 树莓派3利用VNC实现远程桌面
- 使用NOPI实现导出已有固定模板的Excel报表