zoukankan      html  css  js  c++  java
  • [leetcode]174. 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)

    题意:

    骑士(左上角)要救出公主(右下角),只能从起点开始往右或者往下走。每踩一个格子可能失血(或加血)。骑士能活着(至少保持一滴血不死)救出公主的初始血量至少为多少?

    这个题类似64. Minimum Path Sum , 但更为复杂一点的是,从右下角反推左上角,且至少保持一滴血不死。

    思路:

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

    初始化,

    int[][] dp = new int[row][col];   row = grid.length, col = grid[0].length

    dp[row-1][col-1] = max( 1 - grid[row-1][col-1],  1)   若grid[row-1][col-1] 这格是加血,则反推骑士踩这格之前的血量为1(至少保持一滴血不死)即可。

                                                                             若grid[row-1][col-1] 这格是减血,则反推骑士踩这格之前的血量为1 + 减血血量,即 1 - [row-1][col-1] 

    ?     ?    
    ?     ?     
    ?     ?    6

    是否需要预处理最后一个row: dp[row-1][col],因为矩阵中间的dp[row][col]既可能来自下方,也可能来自右方,所以先预处理仅来自右方的每格结果

    是否需要预处理最后一个col:dp[row][col-1],   因为矩阵中间的dp[row][col]既可能来自下方,也可能来自右方,所以先预处理仅来自下方的每格结果

    
    
    

    代码:

     1 class Solution {
     2     public int calculateMinimumHP(int[][] grid) {
     3         int row = grid.length;  
     4         int col = grid[0].length; 
     5         int[][]dp = new int[row][col];
     6         dp[row-1][col-1] = Math.max (1- grid[row-1][col-1], 1) ; 
     7         
     8         for(int i = row-2; i>=0 ; i--){
     9             dp[i][col-1] = Math.max (dp[i+1][col-1] - grid[i][col-1] , 1) ;
    10         }
    11         for(int j = col-2; j>=0 ; j--){
    12             dp[row-1][j] = Math.max (dp[row-1][j+1] - grid[row-1][j] , 1) ;
    13         }
    14         
    15         for(int i = row-2; i>=0 ; i--){
    16             for(int j = col-2; j>=0 ; j--){
    17                 int down = Math.max (dp[i+1][j] - grid[i][j] , 1) ; 
    18                 int right = Math.max(dp[i][j+1] - grid[i][j], 1);
    19                 dp[i][j] = Math.min(down, right);
    20             }
    21         } 
    22       return dp[0][0];  
    23     }
    24 }
  • 相关阅读:
    Documentum之基础(1)
    tomcat部署项目,war包问题[转]
    AD与LDAP区别
    Microsoft Visual Studio2010--此时无足够的可用内存,无法满足操作的预期要求,可能是由于虚拟地址空间碎片造成的。请稍后重试。
    Oracle 11g服务器安装详细步骤
    Oracle 11g 密码永不过期设置
    oracle 11g dbf数据文件从C盘迁移到D盘
    Swift 简介
    0Day 密码重置dede阿帕奇强制安装
    PS抠图教程_抠图小技巧_常用抠图小技巧_图片结构与配色
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9059166.html
Copyright © 2011-2022 走看看