zoukankan      html  css  js  c++  java
  • LeetCode 174. Dungeon Game

    原题链接在这里:https://leetcode.com/problems/dungeon-game/

    题目:

    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) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.

    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.

    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).

    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 is able to rescue the princess.

    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.

    -2 (K) -3 3
    -5 -10 1
    10 30 -5 (P)

    Notes:

      • The knight's health has no upper bound.
      • 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.

    题解:

    DP, 需要保存当前格到右下格所需要的最小体力.

    递归时, 是Math.min(走右侧最小体力,左下侧最小体力). 

    先出示右下角的点,再初始最后一行和最后一列.

    Note:  1.最后返回的不是dp[0][0], 而是dp[0][0]加一,因为之前求得体力值的最小值是0, 但骑士的体力值必须是正数.

    2. 之所以选择从后往前更新而不是从前往后更新是因为,从前往后更新时求得的局部最优不保证是全局最优。

    AC Java:

     1 class Solution {
     2     public int calculateMinimumHP(int[][] dungeon) {
     3         if(dungeon == null || dungeon.length == 0 || dungeon[0].length == 0){
     4             return 0;
     5         }
     6         
     7         int m = dungeon.length;
     8         int n = dungeon[0].length;
     9         
    10         int [][] dp = new int[m][n];
    11         dp[m-1][n-1] = dungeon[m-1][n-1] < 0 ? -dungeon[m-1][n-1]:0;
    12         for(int i = m-2; i>=0; i--){
    13             dp[i][n-1] = dp[i+1][n-1] - dungeon[i][n-1] > 0 ? dp[i+1][n-1] - dungeon[i][n-1] : 0;
    14         }
    15         for(int j = n-2; j>=0; j--){
    16             dp[m-1][j] = dp[m-1][j+1] - dungeon[m-1][j] > 0 ? dp[m-1][j+1] - dungeon[m-1][j] : 0;
    17         }
    18         
    19         for(int i = m-2; i>=0; i--){
    20             for(int j = n-2; j>=0; j--){
    21                 int cost = Math.min(dp[i+1][j], dp[i][j+1]);
    22                 dp[i][j] = cost - dungeon[i][j] > 0 ? cost - dungeon[i][j] : 0;
    23             }
    24         }
    25         return dp[0][0]+1;
    26     }
    27 }
  • 相关阅读:
    快速排序 c++
    编程作业: 感性接触计算机程序
    SAS批量导出sas7bdata至excel
    JetBrains WebStorm 快捷键失效
    grails 解决emoji标签存入mysql
    Groovy使用List集合
    javascript 获取当前部署项目路径
    Groovy基本类型与运算符
    HTML— 弹出遮盖层
    grails 获取domainClassName
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4827823.html
Copyright © 2011-2022 走看看