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) is initially positioned in the top-left, and must fight his, through the dungeon to rescue The princess.
The knight has an initial health point represented by a positive integer. Drops to 0 or below, he dies immediately.
Some of the rooms is guarded by demons, so the knight loses health (negative integers) upon entering these rooms ; Other rooms is either empty (0 ' s) or contain magic orbs that increase the knight ' s Health (positive int Egers).
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's able to rescue the princess.
For example, given the dungeon below, the initial health of the knight must is at least 7 if he follows the optimal path c0/>.
-2 (K) |
-3 |
3 |
-5 |
-10 |
1 |
10 |
30 |
-5 (P) |
Notes:
- The Knight ' s Health has no upper bound.
- Any hostel can contain threats or power-ups, even the first, the knight enters and the Bottom-right, where the Princ ESS is imprisoned.
Test instructions said that, from the upper left corner of a matrix to the lower right corner, each lattice represents the amount of blood need to add or subtract much.
The blood volume is then required to remain positive in the graph, with the smallest amount of blood needed from the upper left to the lower right in case of only right and down.
Obviously with DP, but how to use DP, the first idea is that
1, using two arrays, an array to record the minimum amount of blood required for the current position Min[len], and the other is in the case of the minimum amount of blood on the path of the number and Sum[len].
2, the first line because can only go straight to the right, so very simple.
3, starting from the second line, there are two options, from the top or from the left (the first one separately), and then compare the two options require the minimum amount of blood, choose the smaller path.
4. In the same amount of blood as the two options, select Sum to go larger.
But it's going to go wrong.
[[1,-3,3],[0,-2,0],[-3,-3,-3]] This group will go wrong, so this is wrong. Local optimality is not equal to global optimality.
Public classSolution {//error code Public intCALCULATEMINIMUMHP (int[] Dungeon) { if(Dungeon.length = = 1 && dungeon[0].length = 1){ if(Dungeon[0][0] > 0){ return1; } Else { return-dungeon[0][0]+1; } } intLen = dungeon[0].length; int[] min =New int[Len]; int[] sum =New int[Len]; intnum = dungeon[0][0]; min[0] = Math.max (1,-num + 1); sum[0] =num; for(inti = 1; i < Len; i++) {num+ = Dungeon[0][i]; Sum[i]=num; Min[i]= Math.max (-num + 1, min[i-1]); } intFlag = 0; intFlag2 = 1; for(inti = 1;i < dungeon.length; i++) {sum[0] + = dungeon[i][0]; min[0] = Math.max (min[0],-sum[0] + 1); for(intj = 1;j < Len; J + +){ if(Min[j-1] < Min[j] | | (Min[j-1] = = Min[j] && sum[j-1] >Sum[j])) {Sum[j]= Sum[j-1] +Dungeon[i][j]; MIN[J]= Math.max (Min[j-1],-sum[j] + 1); } Else{Sum[j]+=Dungeon[i][j]; MIN[J]= Math.max (Min[j],-sum[j] + 1); } } } returnMin[len-1]; }}
2, in turn, starting at the end, indicating the minimum amount of blood required for the current position to the end
Public classSolution { Public intCALCULATEMINIMUMHP (int[] Dungeon) { if(Dungeon.length = = 1 && dungeon[0].length = 1){ if(Dungeon[0][0] > 0){ return1; } Else { return-dungeon[0][0] + 1; } } intLen = dungeon[0].length; introw =dungeon.length; int[] min =New int[Len]; Min[len-1] =-dungeon[row-1][len-1] + 1; Min[len-1] = Math.max (min[len-1], 1); for(inti = len-2; I >= 0; i--) {Min[i]= Min[i + 1]-dungeon[row-1][i]; Min[i]= Math.max (Min[i], 1); } for(inti = row-2; I >= 0; i--) {Min[len-1] = min[len-1]-dungeon[i][len-1]; Min[len-1] = Math.max (min[len-1], 1); for(intj = len-2; J >= 0; j--) {Min[j]= Math.min (min[j + 1], min[j])-Dungeon[i][j]; MIN[J]= Math.max (Min[j], 1); } } returnMin[0]; }}
Leetcode 174. Dungeon Game Dungeon Games---------Java