Leetcode -- Dungeon Game
来源:互联网 发布:mysql lpad 编辑:程序博客网 时间:2024/06/10 08:22
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.
class Solution {public: int calculateMinimumHP(vector<vector<int>>& dungeon) { int n=dungeon.size(),m=dungeon[0].size(); vector<vector<int>> f(n,vector<int>(m,0)); f[n-1][m-1]=max(1,1-dungeon[n-1][m-1]); for(int i=m-2;i>=0;--i) f[n-1][i]=max(1,f[n-1][i+1]-dungeon[n-1][i]); for(int i=n-2;i>=0;--i) f[i][m-1]=max(1,f[i+1][m-1]-dungeon[i][m-1]); for(int i=n-2;i>=0;--i) for(int j=m-2;j>=0;--j) { int t1=max(1,f[i][j+1]-dungeon[i][j]); int t2=max(1,f[i+1][j]-dungeon[i][j]); f[i][j]=min(t1,t2); } return f[0][0]; }};
- [leetcode] Dungeon Game
- leetcode 174: Dungeon Game
- [leetcode 174] Dungeon Game
- [leetcode] Dungeon Game
- Leetcode Dungeon Game
- LeetCode Dungeon Game
- LeetCode: Dungeon Game
- [LeetCode] Dungeon Game
- [leetcode] Dungeon Game
- LeetCode Dungeon Game
- [LeetCode]Dungeon Game
- Leetcode OJ Dungeon Game
- Leetcode: Dungeon Game
- leetcode dungeon game
- [leetcode]Dungeon Game
- LeetCode(174) Dungeon Game
- LeetCode 174 Dungeon Game
- LeetCode - Dungeon Game
- Opencv 无按钮设置
- android fragment带实例详解(上)
- 1.http://www.cnblogs.com/wsq724439564/p/3301943.html
- Git版本控制与github使用
- Deep Learning: Doubly Easy and Doubly Powerful with GraphLab Create
- Leetcode -- Dungeon Game
- Linux学习总结之旅-1、在Vmware中安装CentOS-6.5
- C# 对文本文件的几种读写方法总结
- 简单谈谈MVC模式
- java 类的实例化
- 如何在 CentOS 中设置 NTP 服务器
- C语法_文件系统
- how to update ruby
- 查看sqlserver默认的编码格式