|
2 | 2 |
|
3 | 3 | importcom.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 ofm 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 ofM 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)-33 |
19 |
| --5-101 |
20 |
| -1030-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)-33 |
| 20 | +-5-101 |
| 21 | +1030-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 | publicclass_174 {
|
28 |
| - |
29 |
| -/**This problem should fill the dp matrix from bottom right.*/ |
30 |
| -publicintcalculateMinimumHP(int[][]dungeon) { |
31 |
| -if (dungeon ==null ||dungeon.length ==0) { |
32 |
| -return0; |
33 |
| - } |
34 |
| - |
35 |
| -intheight =dungeon.length; |
36 |
| -intwidth =dungeon[0].length; |
37 |
| -int[][]dp =newint[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 (inti =height -2;i >=0;i--) { |
42 |
| -inttemp =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 | +publicstaticclassSolution1 { |
| 32 | +/** This problem should fill the dp matrix from bottom right. */ |
| 33 | +publicintcalculateMinimumHP(int[][]dungeon) { |
| 34 | +if (dungeon ==null ||dungeon.length ==0) { |
| 35 | +return0; |
| 36 | + } |
| 37 | + |
| 38 | +intheight =dungeon.length; |
| 39 | +intwidth =dungeon[0].length; |
| 40 | +int[][]dp =newint[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 (inti =height -2;i >=0;i--) { |
| 46 | +inttemp =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 (intj =width -2;j >=0;j--) { |
| 52 | +inttemp =dp[height -1][j +1] -dungeon[height -1][j]; |
| 53 | +dp[height -1][j] =Math.max(temp,1); |
| 54 | + } |
| 55 | + |
| 56 | +for (inti =height -2;i >=0;i--) { |
47 | 57 | for (intj =width -2;j >=0;j--) {
|
48 |
| -inttemp =dp[height -1][j +1] -dungeon[height -1][j]; |
49 |
| -dp[height -1][j] =Math.max(temp,1); |
50 |
| - } |
51 |
| - |
52 |
| -for (inti =height -2;i >=0;i--) { |
53 |
| -for (intj =width -2;j >=0;j--) { |
54 |
| -intdown =Math.max(1,dp[i +1][j] -dungeon[i][j]); |
55 |
| -intright =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 |
| -returndp[0][0]; |
62 |
| - } |
63 |
| - |
64 |
| - |
65 |
| -publicstaticvoidmain(String...strings) { |
66 |
| -_174test =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 =newint[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 |
| -publicintcalculateMinimumHP_attemp2_failed(int[][]dungeon) { |
126 |
| -if (dungeon ==null ||dungeon.length ==0) { |
127 |
| -return0; |
128 |
| - } |
129 |
| - |
130 |
| -intheight =dungeon.length; |
131 |
| -intwidth =dungeon[0].length; |
132 |
| -int[][]dp =newint[height][width]; |
133 |
| -dp[0][0] =dungeon[0][0] >0 ?1 :1 -dungeon[0][0]; |
134 |
| - |
135 |
| -//fill the first column |
136 |
| -for (inti =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 (intj =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 (inti =1;i <height;i++) { |
146 |
| -for (intj =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 | +intdown =Math.max(1,dp[i +1][j] -dungeon[i][j]); |
| 59 | +intright =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 |
| -returndp[height -1][width -1]; |
| 64 | +CommonUtils.printMatrix(dp); |
| 65 | +returndp[0][0]; |
153 | 66 | }
|
| 67 | + } |
154 | 68 | }
|