174. Dungeon Game

Source: Internet
Author: User

    /** 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

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.