/** 174. Dungeon Game * 12.30 by Mingyang * Dp[i][j] means the minimum HP required to ensure Knight does not die after entering this lattice. * Dp[i][j] is the minimum health value before he enters (I,J). * If the value of a lattice is negative, then the minimum HP required before entering this lattice is-dungeon[i][j] + 1. If the value of the lattice is not negative, then the minimum HP requirement is 1. * Here you can see that the direction of DP is from the bottom right corner to the upper left corner. First dp[m-1][n-1] = Math.max (1,-dungeon[m-1][n-1] + 1). * The recursive equation is dp[i][j] = Math.max (1, Math.min (Dp[i+1][j], dp[i][j+1])-dungeon[i][j]). */ Public intCALCULATEMINIMUMHP (int[] Dungeon) { intm =dungeon.length; intn = dungeon[0].length; //init DP Table int[] DP =New int[M][n]; Dp[m-1][n-1] = Math.max (1-dungeon[m-1][n-1], 1); //init last row for(inti = m-2; I >= 0; i--) {Dp[i][n-1] = Math.max (dp[i + 1][n-1]-dungeon[i][n-1], 1); } //init last column for(intj = n-2; J >= 0; j--) {dp[m-1][J] = Math.max (dp[m-1][j + 1]-dungeon[m-1][j], 1); } //Calculate DP Table for(inti = m-2; I >= 0; i--) { for(intj = n-2; J >= 0; j--) { intDown = Math.max (Dp[i + 1][j]-dungeon[i][j], 1); intright = Math.max (dp[i][j + 1]-dungeon[i][j], 1); DP[I][J]=Math.min (right, down); } } returnDp[0][0]; } /** Here's a little explanation: dp[m-1][j] = Math.max (dp[m-1][j + 1]-dungeon[m-1][j], 1); * What does that mean? How to dungeon to make positive words means I get extra blood, so I'll subtract from this thing, * but if this is negative, it means I have to lose so much blood, so the last thing I mean is, if it's negative, it gets bigger, then I need to have that much money to survive. The situation is, this point a lot of supplies, so the first one minus is negative, meaning that I only need 1 to survive.*/
174. Dungeon Game