Leetcode Dungeon Game

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/dungeon-game/

This is a DP problem, save the current grid to the bottom right of the minimum required strength, M*n DP array saved.

The update is math.min (take the right hand minimum strength, the lower left side of the minimum strength). Take the right hand minimum strength = Math.max (dp[i][j+1]-dungeon[i][j], 0). Suppose always dp[i][j+1] = 3, dungeon[i][j] =-5, 3-(-5) = 8 is the minimum required to go to the right, but if dp[i][j+1] = 3, dungeon[i][j] = 8, 3-8 = 5, the currently eaten magic Bean adds 8 points Physical strength, you do not need the minimum physical value can go to the right, so the minimum energy required is 0. Go down similarly.

First show the bottom right point, then the last row and the last column.

Note:1. The last return is not dp[0][0], but dp[0][0] plus one, because the minimum value of the previously obtained physical value is 0, but the knight's physical strength must be a positive number.

2. The reason for choosing to update from back to front rather than from the back is because the local optimal is not guaranteed to be the global optimal when the former is updated.

AC Java:

1  Public classSolution {2      Public intCALCULATEMINIMUMHP (int[] Dungeon) {3         if(Dungeon = =NULL|| Dungeon.length = = 0 | | Dungeon[0].length = = 0){4             return0;5         }6         intm =dungeon.length;7         intn = dungeon[0].length;8         int[] DP =New int[m][n];9Dp[m-1][n-1] = Dungeon[m-1][n-1] < 0? -dungeon[m-1][n-1]:0;Ten          for(inti = m-2; i>=0; i--){ OneDp[i][n-1] = dp[i+1][n-1]-dungeon[i][n-1] > 0? DP[I+1][N-1]-dungeon[i][n-1]: 0; A         } -          for(intj = n-2; j>=0; j--){ -DP[M-1][J] = dp[m-1][j+1]-dungeon[m-1][j] > 0? DP[M-1][J+1]-Dungeon[m-1][j]: 0; the         } -          for(inti = m-2; i>=0; i--){ -              for(intj = n-2; j>=0; j--){ -DP[I][J] = Math.min ((dp[i+1][j]-dungeon[i][j] > 0? DP[I+1][J]-dungeon[i][j]: 0), (dp[i][j+1]-dungeon[i][j] > 0? dp[i][j+1]-dungeon[i][j]: 0)); +             } -         } +         returnDp[0][0]+1; A     } at}

Leetcode Dungeon Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.