|
1 | 1 | # https://leetcode.com/problems/dungeon-game/
|
2 | 2 | #
|
3 | 3 | # The demons had captured the princess (P) and imprisoned her in the
|
4 |
| -# bottom-right corner of a dungeon. The dungeon consists of M x N |
5 |
| -# rooms laid out in a 2D grid. Our valiant knight (K) was initially |
6 |
| -# positioned in the top-left room and must fight his way through the |
7 |
| -# dungeon to rescue the princess. |
| 4 | +# bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid |
| 5 | +# out in a 2D grid. Our valiant knight (K) was initially positioned in the |
| 6 | +# top-left room and must fight his way through the dungeon to rescue the |
| 7 | +# princess. |
8 | 8 | #
|
9 |
| -# The knight has an initial health point represented by a positive |
10 |
| -# integer. If at any point his health point drops to 0 or below, |
11 |
| -# he dies immediately. |
| 9 | +# The knight has an initial health point represented by a positive integer. If |
| 10 | +# at any point his health point drops to 0 or below, he dies immediately. |
12 | 11 | #
|
13 |
| -# Some of the rooms are guarded by demons, so the knight loses health |
14 |
| -# (negative integers) upon entering these rooms; other rooms are |
15 |
| -# either empty (0's) or contain magic orbs that increase the knight's |
16 |
| -# health (positive integers). |
| 12 | +# Some of the rooms are guarded by demons, so the knight loses health (negative |
| 13 | +# integers) upon entering these rooms; other rooms are either empty (0's) or |
| 14 | +# contain magic orbs that increase the knight's health (positive integers). |
17 | 15 | #
|
18 |
| -# In order to reach the princess as quickly as possible, the knight |
19 |
| -# decides to move only rightward or downward in each step. |
| 16 | +# In order to reach the princess as quickly as possible, the knight decides to |
| 17 | +# move only rightward or downward in each step. |
20 | 18 | #
|
21 |
| -# Write a function to determine the knight's minimum initial health |
22 |
| -# so that he is able to rescue the princess. |
| 19 | +# Write a function to determine the knight's minimum initial health so that he |
| 20 | +# is able to rescue the princess. |
23 | 21 | #
|
24 |
| -# For example, given the dungeon below, the initial health of the |
25 |
| -# knight must be at least 7 if he follows the optimal path |
26 |
| -# RIGHT -> RIGHT -> DOWN -> DOWN. |
| 22 | +# For example, given the dungeon below, the initial health of the knight must |
| 23 | +# be at least 7 if he follows the optimal path RIGHT -> RIGHT -> DOWN -> DOWN. |
27 | 24 | #
|
28 | 25 | # +-------+-------+-------+
|
29 | 26 | # | | | |
|
|
42 | 39 | # Notes:
|
43 | 40 | #
|
44 | 41 | # + The knight's health has no upper bound.
|
45 |
| -# + Any room can contain threats or power-ups, even the first room |
46 |
| -# the knight enters and the bottom-right room where the princess |
47 |
| -# is imprisoned. |
| 42 | +# + Any room can contain threats or power-ups, even the first room the |
| 43 | +# knight enters and the bottom-right room where the princess is imprisoned. |
| 44 | +# |
| 45 | +# Credits: |
| 46 | +# |
| 47 | +# Special thanks to @stellari for adding this problem and creating all |
| 48 | +# test cases. |
48 | 49 |
|
49 | 50 |
|
50 | 51 | # @param {Integer[][]} dungeon
|
|
0 commit comments