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.
 
 
从右下角开始进行动态规划
设有m行,n列
 
考虑边界条件:
最底部的元素:
若dungeon[m-1][n-1]>=0,则
dp[m-1][n-1]=0;
若dungeon[m-1][n-1]<0,则
dp[m-1][n-1]=-dungeon[m-1][n-1];
可以简化为dp[m-1][n-1]=max(-dungeon[m-1][n-1],0);
 
最右边一列
若dungeon[m-2][n-1]>=0,则
dp[m-2][n-1]=max(dp[m-1][n-1]-dungeon[m-2][n-1],0);
若dungeon[m-2][n-1]<0,则
dp[m-2][n-1]=dp[m-1][n-1]+abs(dungeon[m-2][n-1]);
                      =dp[m-1][n-1]-dungeon[m-2][n-1];
 
 
化简为dp[m-2][n-1]=max(dp[m-1][n-1]-dungeon[m-2][n-1],0);
 
 
同理最下边一行
dp[m-1][n-2]=dp[m-1][n-1]-dungeon[m-1][n-2];
 
 
对于其他位置的元素
dp[i][j]=max(min(dp[i+1][j],dp[i][j+1])-dungeon,0)
 
 
 class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) { int m=dungeon.size();
int n=dungeon[].size();
vector<vector<int>> dp(m,vector<int>(n)); dp[m-][n-]=max(-dungeon[m-][n-],); for(int i=m-;i>=;i--)
{
dp[i][n-]=max(dp[i+][n-]-dungeon[i][n-],);
} for(int j=n-;j>=;j--)
{
dp[m-][j]=max(dp[m-][j+]-dungeon[m-][j],);
} for(int i=m-;i>=;i--)
{
for(int j=n-;j>=;j--)
{ dp[i][j]=max(min(dp[i+][j],dp[i][j+])-dungeon[i][j],);
}
} return dp[][]+;
}
};

【leetcode】Dungeon Game的更多相关文章

  1. 【leetcode】Dungeon Game (middle)

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. mssql注入

    <%@ Page Language="C#" AutoEventWireup="true" %> <%@ Import Namespace=& ...

  2. Python开发【第十七篇】:MySQL(一)

    一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...

  3. Yii2-Redis使用小记 - Cache(转)

    前些天简单学习了下 Redis,现在准备在项目上使用它了.我们目前用的是 Yii2 框架,在官网搜索了下 Redis,就发现了yii2-redis这扩展. 安装后使用超简单,打开 common/con ...

  4. .NET安全审核检查表

    书籍名称:Web安全设计之道 -.NET代码安全,界面漏洞防范与程序优化   .NET安全审核检查表   检查项 任务描述 设计环节     Security descisions should no ...

  5. [工具]json转类

    摘要 这周在园子看到一篇介绍JsonCSharpClassGenerator这个工具的文章,感觉挺实用的,在现在项目中json用的是最多的,所以在转换对应的类的时候,确实挺频繁,所以就研究了一下这个工 ...

  6. 推荐近期15个 Node.js 开发工具

    近来Node.js 越来月流行了,这个基于Google V8 引擎建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.在本文中,我们列出了2015年最佳的15个 Node.js 开发工具.这些 ...

  7. LYDSY模拟赛day3 平均数

    [ 问题描述]有一天, 小 A 得到了一个长度为 n 的序列.他把这个序列的所有连续子序列都列了出来, 并对每一个子序列都求了其平均值, 然后他把这些平均值写在纸上, 并对它们进行排序,最后他报出了第 ...

  8. cf306 C. Divisibility by Eight(数学推导)

    C. Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. SQL中关于日期的常用方法

    mysql数据库: logintime >= STR_TO_DATE('$$START_TIME','%Y-%m-%d %H:%i:%s') AND logintime < STR_TO_ ...

  10. 7-RandomAccessFile 随机流

    package com.io; import java.io.File; import java.io.FileNotFoundException; import java.io.IOExceptio ...