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.

基本思路:

动态规划

设health[i][j]  为走进dungeon[i][j]的初始血量,且该个血量将能维持到骑士足以走完右下角。

已知条件:骑士走完右下角至少要剩一滴血。即health[m][n-1] = 1。

m为dungeon行数。

也能够设health[m-1][n] = 1。

此值表示走完右下角的剩余血量。

同一时候也是从该右下角向右,或者向下。走到还有一格时的初始血量。 当然此两格是虚拟的,地牢中不存在。或者形象的说,从右下角向右或者向下走出地牢后,剩余的血量。

从此点。能够倒推出health[0][0]。

骑士仅仅能向右,向下右移动。  要知道当前位置的初始血量,仅仅须要知道其右和其下的初始血量,就能够反推出。

health[i][j] = min(health[i+1][j], health[i[j+1])  - dungeon[i][j]

因为骑士要时刻保持血量至少为1. 上面能够改为:

health[i][j] = max(1, min(health[i+1][j], health[i[j+1])  - dungeon[i][j])

因为递推时仅仅须要其右和其下,两个位置, 能够使用滚动数组。用一维替换掉二维数组。


class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
if (dungeon.empty() || dungeon[0].empty())
return 0;
const int m = dungeon.size();
const int n = dungeon[0].size();
vector<int> health(n+1, INT_MAX);
health[n-1] = 1;
for (int i=m-1; i>=0; i--) {
for (int j=n-1; j>=0; j--) {
health[j] = max(1, min(health[j], health[j+1]) - dungeon[i][j]);
}
}
return health[0];
}
};

Dungeon Game -- latched的更多相关文章

  1. [LeetCode] Dungeon Game 地牢游戏

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

  2. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  3. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java

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

  5. leetcode174. Dungeon Game

    // learn from https://discuss.leetcode.com/topic/6912/c-dp-solution ''' class Solution { public: int ...

  6. 【leetcode】Dungeon Game

    Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...

  7. Dungeon Game ——动态规划

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

  8. Java for LeetCode 174 Dungeon Game

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

  9. LeetCode Dungeon Game

    原题链接在这里:https://leetcode.com/problems/dungeon-game/ 这是一道DP题,保存当前格到右下格所需要的最小体力,m*n的dp数组保存. 更新是Math.mi ...

随机推荐

  1. SQLiteOpenHelper学习

    0.视频:http://www.imooc.com/video/3384 1.SQLiteOpenHelper笔记: 2.SQLiteOpenHelper.java代码: import android ...

  2. Qt杂记——布局、信号与槽等

    1.QHBoxLayout布局设置拉伸: ui->TopLayout->setStretch(,); //left ui->TopLayout->setStretch(,); ...

  3. 部署bugzilla(bugzilla+apache+mysql+linux)

    工作原因,需要部署bugzilla.在此,容我新造个轮子.官方轮子:https://bugzilla.readthedocs.org/en/latest/installing/quick-start. ...

  4. VMWare 支持的网络连接类型 (VMWare Virtual Network Connection Types)

  5. SDK _ 静态控件的使用

    静态控件的使用 静态控件主要区分两种使用方式:文本 \ 图片 在使用静态控件的时候,ID始终默认为 IDC_STATIC,需要进行更改 怎样通过可视化编程显示一张图片 需要添加一个位图资源 需要添加一 ...

  6. 16位/32位/64位CPU的位究竟是说啥

    平时,我们谈论CPU,都会说某程序是32位编译,可以跑在32位机或64位机,或则是在下载某些开源包时,也分32位CPU版本或64CPU位版本,又或者在看计算机组成相关书籍时,特别时谈到X86 CPU时 ...

  7. HDU多校Round 4

    Solved:3 rank:405................................. B. Harvest of Apples 知道了S(n,m) 可以o(1)的求S(n - 1, m ...

  8. 网络编程 - socket接收大数据

    通过socket,实现客户端发送命令,将服务端执行出的结果,反回到客户端,主要4个步骤:1.服务端返回数据: 2.服务端返回数据的大小: 3.客户端接收返回数据的大小: 4.客户端按返回数据大小接收数 ...

  9. 一个小demo熟悉Spring Boot 和 thymeleaf 的基本使用

    目录 介绍 零.项目素材 一. 创建 Spring Boot 项目 二.定制首页 1.修改 pom.xml 2.引入相应的本地 css.js 文件 3.编辑 login.html 4.处理对 logi ...

  10. with一个对象,自动触发__enter__方法

    class Foo(object): def __init__(self): pass def __enter__(self): print("__enter__") def __ ...