zoukankan      html  css  js  c++  java
  • Dungeon Game 解答

    Question

    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.

    Solution

    这道题一看就知道用DP做。但是如果是从(0,0)开始推到(m - 1, n - 1),对于每个点,我们需要纪录两个值: 1. 走到该点需要的最少血量 2. 该点富余的血量。这样,我们无法得到一个判断标准。

    所以,我们从反方向出发。从(m - 1, n - 1)到(0,0),hp[i][j] 代表从(i,j)走到终点需要的最少血量。如果dungeon[i][j]的值为负,那么进入这个格子之前knight需要有的最小HP是-dungeon[i][j] + min(left, right).如果格子的值非负,那么最小HP需求就是1。

     1 public class Solution {
     2     public int calculateMinimumHP(int[][] dungeon) {
     3         int m = dungeon.length, n = dungeon[0].length;
     4         int[][] hp = new int[m][n];
     5         hp[m - 1][n - 1] = Math.max(-dungeon[m - 1][n - 1] + 1, 1);
     6         for (int i = m - 2; i >= 0; i--) {
     7             hp[i][n - 1] = Math.max(-dungeon[i][n - 1] + hp[i + 1][n - 1], 1);
     8         }
     9         for (int i = n - 2; i >= 0; i--) {
    10             hp[m - 1][i] = Math.max(-dungeon[m - 1][i] + hp[m - 1][i + 1], 1);
    11         }
    12         
    13         for (int i = m - 2; i >= 0; i--) {
    14             for (int j = n - 2; j >= 0; j--) {
    15                 hp[i][j] = Math.max(1, -dungeon[i][j] + Math.min(hp[i + 1][j], hp[i][j + 1]));
    16             }
    17         }
    18         return hp[0][0];
    19     }
    20 }
  • 相关阅读:
    UE4 ios环境搭建备忘
    我所理解的打击感 逐帧分析过几十款游戏的开发者经验分享
    可重入锁和不可重入锁
    Java中JDK和JRE的区别是什么?它们的作用分别是什么?
    Java开发岗位面试题
    详解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
    docker:轻量级图形页面管理工具Portainer
    Docker整合dockerfly实现UI界面管理(单机版)
    docker for mac的JSON配置文件中的hosts项修改后无法生效
    Maven [ERROR] 不再支持源选项 5。请使用 6 或更高版本
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4889357.html
Copyright © 2011-2022 走看看