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 (开始点) -3            3               
-5 -10 1
10 30            -5(结束点)
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.
题目大致意思:

恶魔抓走了公主(P)并把她囚禁在地牢的右下角。地牢包含M x N个房间,排列成一个二维的格子。我们英勇的骑士(K)一开始位于左上角的房间里,需要一路披荆斩棘营救公主。骑士拥有一个正整数的初始生命值。如果在任何一点其生命值≤0,他立刻会死去。一些房间由恶魔守卫着,因此当骑士进入这些房间时就会损失生命值(负整数);其他房间或者是空的(数值为0),或者包含一些魔力宝珠(magic orbs)可以增加骑士的生命值(正整数)。为了尽可能快的解救公主,骑士决定每一步只向右或者向下移动。编写一个函数决定骑士的最小初始生命值,确保他可以成功营救公主。例如,给定下面的地牢,骑士的初始生命值至少应当为7,如果他按照下面的最优路线行进:右 -> 右 -> 下 -> 下.
-2 (开始点) -3            3               
-5 -10 1
10 30            -5(结束点)
备注:骑士的生命值没有上界任何房间都可能包含威胁或者补给,即使骑士进入的第一个房间或者囚禁公主的最后一个房间也一样。

思路:从表格开始入手,因为要使得达到最后的节点(结束点)血量大于0

-2 (开始点) -3            3               
-5 -10 1
10 30            -5(结束点)
我们定义一个数组dp[i][j],表示骑士在点i,j处至少需要的血量,又因为血量必须大于0,并且从最后一个点往前面类推,运用动态规划的思路,对于结束点,我们有如下dp数组

(开始点)    
     
    6(结束点)
这里的6怎么来的:因为在最后一格需要消耗-5,那么骑士达到此处是至少需要6点血量,使得6+(-5)=1>0,这样才不会死掉

这样我们可以类推,最后一行,和最后一列的数据

为什么是最后一行,最后一列,因为如果骑士此时处于最后一行或者最后一列的某一个点,只有一条路径达到结束点(最后一行,往右走,最后一列,往下走)
  for(int i=row-2;i>=0;i--)dp[i][col-1]=Max(dp[i+1][col-1]-dungeon[i][col-1],1);for(int i=col-2;i>=0;i--)dp[row-1][i]=Max(dp[row-1][i+1]-dungeon[row-1][i],1);
初始化得到最后一行,和最后一列的数据

(开始点)   2              
    5
1 1            6(结束点)
然后根据求的的最后一行和最后一列的数据,用动态规划思想,求的其他点的最少血量

7(开始点) 5           2              
6 11            5
1 1            6(结束点)

最后返回 dp[0][0]即可

代码如下:


class Solution {public:int calculateMinimumHP(vector<vector<int>>& dungeon) {int row=dungeon.size();int col=dungeon[0].size();int dp[row][col]; //表示到某一点是至少需要 的 血 ,从最后一个点开始,最少需要6,因为6-5=1>0dp[row-1][col-1]=Max(1-dungeon[row-1][col-1],1) ;//表示最后一个需要的for(int i=row-2;i>=0;i--)dp[i][col-1]=Max(dp[i+1][col-1]-dungeon[i][col-1],1);for(int i=col-2;i>=0;i--)dp[row-1][i]=Max(dp[row-1][i+1]-dungeon[row-1][i],1);for(int i=row-2;i>=0;i--){for(int j=col-2;j>=0;j--){int right=Max(dp[i][j+1]-dungeon[i][j],1);int down=Max(dp[i+1][j]-dungeon[i][j],1);dp[i][j]=Min(right,down);}}return dp[0][0];}int Max(int a,int b){return a>b?a:b;}int Min(int a,int b){return a>b?b:a;}
};

更多解题思路:https://github.com/bigfishman/Myleetcode-study/blob/master/README.md

Dungeon Game相关推荐

  1. 174. Dungeon Game

    一.题目 1.审题 2.分析 只能向右.向下移动的王子,从左上角要到右下角救公主,每经过一个方格,可能获得血瓶加血量,或者碰到怪物减血量,当王子血量 < 1 时就挂了,为了能成功救得公主,求王子 ...

  2. 超 31% 生成文本涉暴力色情,GPT-3 模型在 AI Dungeon 游戏里疯狂「飙车」!

    点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 转自 | 新智元 来源 | GitHub 编辑 | LRS AI被人 ...

  3. Dungeon Master(bfs)广度优先搜索

    描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of ...

  4. B - Dungeon Master POJ - 2251

    B - Dungeon Master POJ - 2251 题目: 逃离3D迷宫,n <=30 首先是dfs,算一下最坏情况共30层,每层有30*30的循环, 复杂度 3030*30 显然严重爆 ...

  5. GPT-2的大规模部署:AI Dungeon 2 如何支撑百万级用户

    2020-02-14 11:16 导语:这是一个经典的文本冒险游戏 早在 2019 年 3 月,我就建立了一个名为 AI Dungeon 的 hackathon 项目.这个项目是一个经典的文本冒险游戏 ...

  6. UVA 532 - Dungeon Master

    六个方向广搜...先是knowledgetime的代码,我写了一遍能A... #include <stdio.h>#include <string.h>#define Msiz ...

  7. (广搜)Dungeon Master -- poj -- 2251

    链接: http://poj.org/problem?id=2251 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2137 ...

  8. Dungeon Master(三维bfs)java

    题意: You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed o ...

  9. nyoj3533D dungeon

    3D dungeon 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 输入 The input consists of a number of dungeons. Each d ...

  10. POJ 2251 Dungeon Master(三维BFS求最短路径)

    3D dungeon 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 [提交][状态][讨论版][命题人:201506020829][Edit] [TestData] 题 ...

最新文章

  1. 抽象工厂模式_常用设计模式--抽象工厂模式
  2. Ansible批量添加远程登录用户
  3. Spring Aspect 获取请求参数
  4. Speex回声消除原理深度解析
  5. python文件打包成exe是 upx不可用、找不到py文件_使用PyInstaller将Python程序打包成一个单独的exe文件...
  6. Python做数据分析时中文乱码?matplotlib出现中文乱码3行代码解决
  7. 基于.NetCore3.1搭建项目系列 —— 使用Swagger做Api文档(上篇)
  8. 飞畅科技-工业以太网交换机组网方式介绍
  9. 梯度提升树(GBDT)原理小结(转载)
  10. 计算同比和环比的区别_【数据说第三期】同比和环比数据分析时,有哪些需要注意的点?...
  11. hdu 1261 字串数
  12. 计算机字体安装按钮灰色,win10系统无法安装字体安装按钮灰色的的解决方法
  13. android应用程序在哪找,找不到应用程序的错误android
  14. ChromeOS+Win双系统安装教程
  15. 扫描枪取消回车二维码_扫码枪设置自动回车方法步骤,条码扫描枪不自动回车怎么办...
  16. 4.1.5 消费者获取记录
  17. MATLAB图像如何显示希腊字母、上下标(alpha、beta等)
  18. 采用seam2的工具创建seam工程
  19. 一位高中竞赛蒟蒻的大学C++学习日记-第三篇-数组、字符、字符串
  20. form表单的enctype

热门文章

  1. kears编写CNN网络,实现对mnist的识别
  2. 应用泛函分析—线性空间
  3. 第三阶段应用层——1.7 数码相册—电子书(1)—实现
  4. matlab表示大于等于,matlab不等于怎么表示
  5. 计算机应用软件弹窗消除,怎么去掉电脑弹窗广告?这几种方法教你轻松解决
  6. 时间工具类封装以及时间戳之间的相互转换
  7. python俄罗斯方块小游戏实验报告_python实现俄罗斯方块游戏
  8. VirtualBox简单使用
  9. 关于define与defined的区别
  10. 字节和兆字节的换算_兆字节(MB)中有多少个字节?