|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.utils.CommonUtils; |
4 | 4 |
|
5 | | -/**174. Dungeon Game |
| 5 | +/** |
| 6 | + * 174. Dungeon Game |
6 | 7 |
|
7 | | -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. |
| 8 | + 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. |
8 | 9 |
|
9 | | -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. |
| 10 | + 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. |
10 | 11 |
|
11 | | -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). |
| 12 | + 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). |
12 | 13 |
|
13 | | -In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step. |
| 14 | + In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step. |
14 | 15 |
|
15 | | -Write a function to determine the knight's minimum initial health so that he is able to rescue the princess. |
| 16 | + Write a function to determine the knight's minimum initial health so that he is able to rescue the princess. |
16 | 17 |
|
17 | | -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. |
18 | | --2 (K) -3 3 |
19 | | --5 -10 1 |
20 | | -10 30 -5 (P) |
| 18 | + 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. |
| 19 | + -2 (K) -3 3 |
| 20 | + -5 -10 1 |
| 21 | + 10 30 -5 (P) |
21 | 22 |
|
22 | | -Notes: |
| 23 | + Note: |
| 24 | +
|
| 25 | + The knight's health has no upper bound. |
| 26 | + 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. |
23 | 27 |
|
24 | | - The knight's health has no upper bound. |
25 | | - 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. |
26 | 28 | */ |
27 | 29 | public class _174 { |
28 | | - |
29 | | - /**This problem should fill the dp matrix from bottom right.*/ |
30 | | - public int calculateMinimumHP(int[][] dungeon) { |
31 | | - if (dungeon == null || dungeon.length == 0) { |
32 | | - return 0; |
33 | | - } |
34 | | - |
35 | | - int height = dungeon.length; |
36 | | - int width = dungeon[0].length; |
37 | | - int[][] dp = new int[height][width]; |
38 | | - dp[height - 1][width - 1] = (dungeon[height - 1][width - 1] > 0) ? 1 : 1 - dungeon[height - 1][width - 1]; |
39 | | - |
40 | | - //fill the last column |
41 | | - for (int i = height - 2; i >= 0; i--) { |
42 | | - int temp = dp[i + 1][width - 1] - dungeon[i][width - 1]; |
43 | | - dp[i][width - 1] = Math.max(1, temp); |
44 | | - } |
45 | 30 |
|
46 | | - //fill the last row |
| 31 | + public static class Solution1 { |
| 32 | + /** This problem should fill the dp matrix from bottom right. */ |
| 33 | + public int calculateMinimumHP(int[][] dungeon) { |
| 34 | + if (dungeon == null || dungeon.length == 0) { |
| 35 | + return 0; |
| 36 | + } |
| 37 | + |
| 38 | + int height = dungeon.length; |
| 39 | + int width = dungeon[0].length; |
| 40 | + int[][] dp = new int[height][width]; |
| 41 | + dp[height - 1][width - 1] = |
| 42 | + (dungeon[height - 1][width - 1] > 0) ? 1 : 1 - dungeon[height - 1][width - 1]; |
| 43 | + |
| 44 | + //fill the last column |
| 45 | + for (int i = height - 2; i >= 0; i--) { |
| 46 | + int temp = dp[i + 1][width - 1] - dungeon[i][width - 1]; |
| 47 | + dp[i][width - 1] = Math.max(1, temp); |
| 48 | + } |
| 49 | + |
| 50 | + //fill the last row |
| 51 | + for (int j = width - 2; j >= 0; j--) { |
| 52 | + int temp = dp[height - 1][j + 1] - dungeon[height - 1][j]; |
| 53 | + dp[height - 1][j] = Math.max(temp, 1); |
| 54 | + } |
| 55 | + |
| 56 | + for (int i = height - 2; i >= 0; i--) { |
47 | 57 | for (int j = width - 2; j >= 0; j--) { |
48 | | - int temp = dp[height - 1][j + 1] - dungeon[height - 1][j]; |
49 | | - dp[height - 1][j] = Math.max(temp, 1); |
50 | | - } |
51 | | - |
52 | | - for (int i = height - 2; i >= 0; i--) { |
53 | | - for (int j = width - 2; j >= 0; j--) { |
54 | | - int down = Math.max(1, dp[i + 1][j] - dungeon[i][j]); |
55 | | - int right = Math.max(1, dp[i][j + 1] - dungeon[i][j]); |
56 | | - dp[i][j] = Math.min(down, right); |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - CommonUtils.printMatrix(dp); |
61 | | - return dp[0][0]; |
62 | | - } |
63 | | - |
64 | | - |
65 | | - public static void main(String... strings) { |
66 | | - _174 test = new _174(); |
67 | | -// int[][] dungeon = new int[1][1]; |
68 | | -// dungeon[0][0] = 0; |
69 | | - |
70 | | -// int[][] dungeon = new int[1][1]; |
71 | | -// dungeon[0][0] = -200; |
72 | | - |
73 | | -// int[][] dungeon = new int[1][2]; |
74 | | -// dungeon[0][0] = 0; |
75 | | -// dungeon[0][1] = -3; |
76 | | - |
77 | | -// int[][] dungeon = new int[2][1]; |
78 | | -// dungeon[0][0] = -3; |
79 | | -// dungeon[1][0] = -7; |
80 | | - |
81 | | - int[][] dungeon = new int[2][1]; |
82 | | - dungeon[0][0] = 2; |
83 | | - dungeon[1][0] = 1; |
84 | | - |
85 | | -// int[][] dungeon = new int[1][2]; |
86 | | -// dungeon[0][0] = -3; |
87 | | -// dungeon[0][1] = 5; |
88 | | - |
89 | | -// int[][] dungeon = new int[2][2]; |
90 | | -// dungeon[0][0] = 2; |
91 | | -// dungeon[0][1] = 1; |
92 | | -// dungeon[1][0] = 1; |
93 | | -// dungeon[1][1] = -1; |
94 | | - |
95 | | -// int[][] dungeon = new int[1][2]; |
96 | | -// dungeon[0][0] = 0; |
97 | | -// dungeon[0][1] = 0; |
98 | | - |
99 | | -// int[][] dungeon = new int[2][1]; |
100 | | -// dungeon[0][0] = 0; |
101 | | -// dungeon[1][0] = 0; |
102 | | - |
103 | | -// int[][] dungeon = new int[3][3]; |
104 | | -// dungeon[0][0] = -2; |
105 | | -// dungeon[0][1] = -3; |
106 | | -// dungeon[0][2] = 3; |
107 | | -// dungeon[1][0] = -5; |
108 | | -// dungeon[1][1] = -10; |
109 | | -// dungeon[1][2] = 1; |
110 | | -// dungeon[2][0] = 10; |
111 | | -// dungeon[2][1] = 30; |
112 | | -// dungeon[2][2] = -5; |
113 | | - |
114 | | -// int[][] dungeon = new int[2][3]; |
115 | | -// dungeon[0][0] = 0; |
116 | | -// dungeon[0][1] = 0; |
117 | | -// dungeon[0][2] = 0; |
118 | | -// dungeon[1][0] = 1; |
119 | | -// dungeon[1][1] = 1; |
120 | | -// dungeon[1][2] = -1; |
121 | | - CommonUtils.printMatrix(dungeon); |
122 | | - System.out.println(test.calculateMinimumHP(dungeon)); |
123 | | - } |
124 | | - |
125 | | - public int calculateMinimumHP_attemp2_failed(int[][] dungeon) { |
126 | | - if (dungeon == null || dungeon.length == 0) { |
127 | | - return 0; |
128 | | - } |
129 | | - |
130 | | - int height = dungeon.length; |
131 | | - int width = dungeon[0].length; |
132 | | - int[][] dp = new int[height][width]; |
133 | | - dp[0][0] = dungeon[0][0] > 0 ? 1 : 1 - dungeon[0][0]; |
134 | | - |
135 | | - //fill the first column |
136 | | - for (int i = 1; i < height; i++) { |
137 | | - dp[i][0] = dungeon[i][0] >= 0 ? dp[i - 1][0] : -dungeon[i][0] + dp[i - 1][0]; |
138 | | - } |
139 | | - |
140 | | - //fill the first row |
141 | | - for (int j = 1; j < width; j++) { |
142 | | - dp[0][j] = dungeon[0][j] >= 0 ? dp[0][j - 1] : -dungeon[0][j] + dp[0][j - 1]; |
143 | | - } |
144 | | - |
145 | | - for (int i = 1; i < height; i++) { |
146 | | - for (int j = 1; j < width; j++) { |
147 | | - dp[i][j] = dungeon[i][j] >= 0 ? Math.min(dp[i][j - 1], dp[i - 1][j]) : -dungeon[i][j] + Math.min(dp[i][j - 1], dp[i - 1][j]); |
148 | | - } |
| 58 | + int down = Math.max(1, dp[i + 1][j] - dungeon[i][j]); |
| 59 | + int right = Math.max(1, dp[i][j + 1] - dungeon[i][j]); |
| 60 | + dp[i][j] = Math.min(down, right); |
149 | 61 | } |
| 62 | + } |
150 | 63 |
|
151 | | - CommonUtils.printMatrix(dp); |
152 | | - return dp[height - 1][width - 1]; |
| 64 | + CommonUtils.printMatrix(dp); |
| 65 | + return dp[0][0]; |
153 | 66 | } |
| 67 | + } |
154 | 68 | } |
0 commit comments