Leetcode: Dungeon Game
来源:互联网 发布:开网店软件可靠吗 编辑:程序博客网 时间:2024/05/17 04:49
分析:
At first glance, this problem bears a large resemblance to the "Maximum/Minimum Path Sum" problem. However, a path with maximum overall health gain does not guarantee the minimum initial health, since it is essential in the current problem that the health never drops to zero or below. For instance, consider the following two paths: 0 -> -300 -> 310 -> 0
and 0 -> -1 -> 2 -> 0
. The net health gain along these paths are -300 + 310 = 10
and -1 + 2 = 1
, respectively. The first path has the greater net gain, but it requires the initial health to be at least 301 in order to balance out the -300 loss in the second room, whereas the second path only requires an initial health of 2.
Fortunately, this problem can be solved through a table-filling Dynamic Programming technique, similar to other "grid walking" problems:
To begin with, we should maintain a 2D array
D
of the same size as the dungeon, whereD[i][j]
represents the minimum health that guarantees the survival of the knight for the rest of his quest BEFORE entering room(i, j)
. ObviouslyD[0][0]
is the final answer we are after. Hence, for this problem, we need to fill the table from the bottom right corner to left top.Then, let us decide what the health should be at least when leaving room
(i, j)
. There are only two paths to choose from at this point:(i+1, j)
and(i, j+1)
. Of course we will choose the room that has the smallerD
value, or in other words, the knight can finish the rest of his journey with a smaller initial health. Therefore we have:min_HP_on_exit = min(D[i+1][j], D[i][j+1])
Now
D[i][j]
can be computed fromdungeon[i][j]
andmin_HP_on_exit
based on one of the following situations:- If
dungeon[i][j] == 0
, then nothing happens in this room; the knight can leave the room with the same health he enters the room with, i.e.D[i][j] = min_HP_on_exit
. - If
dungeon[i][j] < 0
, then the knight must have a health greater thanmin_HP_on_exit
before entering(i, j)
in order to compensate for the health lost in this room. The minimum amount of compensation is "-dungeon[i][j]
", so we haveD[i][j] = min_HP_on_exit - dungeon[i][j]
. - If
dungeon[i][j] > 0
, then the knight could enter(i, j)
with a health as little asmin_HP_on_exit - dungeon[i][j]
, since he could gain "dungeon[i][j]
" health in this room. However, the value ofmin_HP_on_exit - dungeon[i][j]
might drop to 0 or below in this situation. When this happens, we must clip the value to 1 in order to make sureD[i][j]
stays positive:D[i][j] = max(min_HP_on_exit - dungeon[i][j], 1)
.
Notice that the equation for
dungeon[i][j] > 0
actually covers the other two situations. We can thus describe all three situations with one common equation, i.e.:D[i][j] = max(min_HP_on_exit - dungeon[i][j], 1)
for any value of
dungeon[i][j]
.- If
Take
D[0][0]
and we are good to go. Also, like many other "table-filling" problems, the 2D arrayD
can be replaced with a 1D "rolling" array here.
转载自https://leetcode.com/problems/dungeon-game/solution/
- [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
- 商品分类插入商品父类触发器
- map
- 《Python核心编程》数字类型
- android Intent机制详解
- Lucene关系数据库的使用
- Leetcode: Dungeon Game
- MATLAB日记01
- 验证码生成和验证servlet
- [NOI2011]智能车比赛(计算几何+动态规划)
- html5学习渐阶笔记---样式,链接,表格
- C#做的CPU内存使用率
- Java EE JBoss AS8 wildfly-8.2.0 配置 MySQL 3.1.14 数据源
- Zorka监控平台的Online reconfiguration基本效果展示
- 分页的制作java类