leetcode 174. Dungeon Game

【Leetcode】174. 地牢游戏(Dungeon Game) Leetcode - 174 Dungeon Game (Hard) 题目描述:给定一个矩阵,每个位置有正数加血,负数减血,骑士要从左上角走到右下角,只能向下或向右移动,骑士血量小于等于 0 就死亡,求骑士达到右下角需要的最小初始血量。 解题思路:从终点向起点逆推,定义 dp[i][j] 表示 (i, j) 为起点时需要的初始血量,才能从 (1,j) 走到终点。应该取走下面和走右边的最小值,即 d... 阅读详情

The demons had captured the princess § 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.

Note:

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.

在这里插入图片描述

骑士从左上角的(K)出发,(Knight: 骑士),走到正数的格子是加那么多数值的血量,负数的格子是掉血,问从左上角出发走到右下角解救公主(P:Princess)的位置,刚开始左上角的位置至少需要多少血量。
注意在任意一格如果血量降为0,就挂掉了,不能再继续往前走,所以每一格的血量最少是1

思路:
首先看最后P的位置,到达这个位置减掉这个格子的血量之后,至少要剩余1的血量
x - 5 = 1 -> x = 6, 所以要带着6的血量进入P的格子,才不会挂掉

倒推:现在从P上面一格出发,进入这一格会+1, 要保证在这一格保留6的血量才能进入P,所以x+1 = 6 -> x = 5

所以定义一个dp数组,假设dp[i][j]表示从[i][j]出发需要的血量,那么
向右走时: dp[i][j] + dungeon[i][j] = dp[i][j+1]
向下走时: dp[i][j] + dungeon[i][j] = dp[i+1][j]
而只需要在这两种路径中取较小的
所以:
dp[i][j] = min(dp[i][j+1], dp[i+1][j]) -dungeon[i][j]
另外还要保证dp[i][j]至少是1

边界情况只需要考虑一个方向就行了

所以可以得到以下数组:

752
6115
116

最后返回dp[0][0]就行了,就是从[0][0]出发时的血量

    public int calculateMinimumHP(int[][] dungeon) {
        if (dungeon == null || dungeon.length == 0 || dungeon[0].length == 0) {
            return 0;
        }
        
        int height = dungeon.length;
        int width = dungeon[0].length;
        
        int[][] dp = new int[height][width];
        
        //P position..
        dp[height - 1][width - 1] = Math.max(1, 1 - dungeon[height - 1][width - 1]);
        
        //right border..
        for (int i = height - 2; i >= 0; i--) {
            dp[i][width - 1] = Math.max(1, dp[i + 1][width - 1] - dungeon[i][width - 1]);
        }
        
        //down border..
        for (int j = width - 2; j >= 0; j--) {
            dp[height - 1][j] = Math.max(1, dp[height - 1][j + 1] - dungeon[height - 1][j]);
        }
        
        //other grids..
        for (int i = height - 2; i >= 0; i--) {
            for (int j = width - 2; j >= 0; j--) {
                dp[i][j] = Math.min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j];
                dp[i][j] = Math.max(1, dp[i][j]);
            }
        }
        
        return dp[0][0];
    }
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 p 阅读详情

相关推荐

从PWM到正弦波:RC低通滤波器的魔法转换之旅

本文详细介绍了如何利用RC低通滤波器将STM32F103生成的PWM信号转换为高质量正弦波的技术原理与实践方法。通过分析滤波器设计、参数计算、硬件实现及Proteus仿真验证,为嵌入式开发者提供了一套完整的PWM转正弦波解决方案,适用于电机控制、音频信号生成等多种应用场景。

jupi8的博客 382

[leetcode] 174. Dungeon Game 解题报告

题目链接:https://leetcode.com/problems/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

小榕流光的专栏 3641

《最游记》游戏开发全栈指南:从源码解析到实战部署

本文提供了一份详尽的《最游记》游戏源码全栈开发指南。文章深入解析了这款3D玄幻网游的完整源码结构,涵盖客户端、服务端、引擎和工具链,并提供了从环境搭建、核心模块解析到实战修改与部署的完整流程。对于希望深入理解MMORPG架构的开发者而言,这份指南是宝贵的实战资源。

weixin_28363123的博客 75

LeetCode174. Dungeon Game

LeetCode174. Dungeon Game 问题来源LeetCode174 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

yanqinghe123的专栏 370

保姆级教程:用达芬奇Configurator配置CAN总线(从DBC导入到Controller设置)

本文详细介绍了使用达芬奇Configurator工具从DBC文件导入到CAN总线控制器设置的全流程配置方法。通过环境准备、CAN控制器模式选择、硬件参数配置、通信参数优化及通信栈集成等关键步骤,帮助开发者高效完成AUTOSAR架构下的CAN总线开发,特别适合汽车电子领域的工程师参考。

weixin_30529023的博客 443

Win10安装Nek5000

1 安装wsl 2 2 安装Nek5000 3 安装依赖项 4 安装openmpi 5 配置Nek5000 6 编译Nek5000 7 运行Nek5000算例 8 安装paraview并查看结果

weixin_43176703的博客 3451

【Leetcode】174. Dungeon Game

题目地址: https://leetcode.com/problems/dungeon-game/ 给定一个二维矩阵AAA,一个骑士从左上角出发,只能向右或者向下走一格,要求走到右下角。每次走到一格的时候,他的血量就会改变当前格子里的数字那么多,数字是正的血量就增加,否则就减少。要求路径上不能血量变为000或负。问他出发时候的血量至少要是多少。左上角出发时候的格子也会影响血量。 思路是动态规划。设f[i][j]f[i][j]f[i][j]是到达A[i][j]A[i][j]A[i][j]之前必须至少要有的血量

数学、算法爱好者的博客 225

看完让你的RSA提升一个台阶 [GKCTF 2021]RRRRsa

我是,今天我给大家分享一个关于RSA的题目,带大家领略的魅力。今天开始前,我们先学习一下。

m0_62102520的博客 853

leetcode 174: Dungeon Game

Dungeon Game Total Accepted: 332 Total Submissions: 2130 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 ro

西施豆腐渣 2555

一生一芯——用RTL实现最简单的处理器NPC(南大数逻计组RV32ICPU) 代码及思考参考

本文介绍自己前期用Verilog编写的基于RV32I指令集的CPU,个人认为是最简单最易懂的版本(即没做什么优化,即代码很水,没有考虑一些奇怪的情况,只适用简单指令序列,即题目所示最简单的处理器),适合新手练手Verilog以及了解简单CPU的运行过程。本文基于一生一芯,利用Verilator进行仿真,并用gtkwave查看波形,不了解verilator和gtkwave的可以看前几篇文章。

long0_o的博客 3313

STM32F401RCT6最小系统原理图设计

STM32F401RCT6最小系统原理图设计一、主控芯片二、电源电路三、晶振电路三、下载电路四、完整电路图      本文章基于STM32F401RCT6进行最小系统原理图设计,包含主控芯片,电源,晶振电路,下载电路等几部分 一、主控芯片 主控芯片采用STM32F401RCT6,需注意: VBAT在不接电池的情况下需接3.3V,VDDAVREF+需接3.3V,否则会造成芯片供电不正常,无法启动和识别的情况 VDDAVREF+和VCAP1处电容需按照规格书

weixin_43278295的博客 2万+

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 initia

BUILD 1753

【Unity】Gif图片播放插件(支持UI及模型纹理渲染两种模式)

unity本身不支持GIF格式文件直接渲染, 因此一般的做法是通过加载图片,通过代码控制其循环播放、停止等,模拟动画效果,本文简单介绍一下某个现有开源工具,感谢开源作者。

灵境引路人 4036

[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 pos

coder 进阶的专栏 755

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

LeetCode174—Dungeon Game

LeetCode174 Dungeon Game

上善若水 582

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

LeetCode - 解题笔记 - 174 - Dungeon Game

【官方题解参考】正眼一看就会知道是动态规划,但是实际考虑的时候会发现一个问题:从左上到右下的方向的规划计算无法在后续确认的情况下完全确认。最优路径会随着后续的结果的选择而不断发生变化,无后效性不成立。反过来看,实际上本体就是在求取初始值。因此如果将规划方向从左上变成右下,整个逻辑就通了……最优子结构:dp[i][j]dp[i][j]dp[i][j],从终点(右下)到达当前点所需要的最小能量值初始值:dp[m−1][n−1]=max⁡(1,1−dungeon[m−1][n−1])dp[m-1][n-1] =

支锦铭的博客 340

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Leetcode——174. Dungeon Game

本周做了一个动态规划的题目: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. Ou

parishong的博客 350

LeetCode 174 Dungeon Game

题目给的条件 1. 二维网格m*n 行列 //m=1 n=1 2. P右下角 3. k左上角 4. k可以向右和向下移动 5. 遇怪掉血 6. 遇药加血 7. 血量<=0骑士会死要求:求出救到公主的最小血量 骑士的健康没有上限 每个房子有怪物或者血瓶 第一个想法:深度优先搜索遍历,从p开始假设救到公主只剩1滴血, 开始搜索,遇到怪加血,遇到血瓶把血

不要挡住我的阳光 387

Leetcode 174. Dungeon Game 地下城游戏

题目:一些恶魔抓住了公主(P)并将她关在了地下城的右下角。地下城是由 M x N 个房间组成的二维网格。我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主。骑士的初始健康点数为一个正整数。如果他的健康点数在某一时刻降至 0 或以下,他会立即死亡。有些房间由恶魔守卫,因此骑士在进入这些房间时会失去健康点数(若房间里的值为负整数,则表示骑士将损失健康点数);其他房...

zhangzhetaojj的博客 987

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 pos...

reigns的博客 285

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 p

无名山丘,崛起成峰 773

LeetCode174 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 pos...

李歇特冯-兹拜因巴哈的博客 158

[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

u014673347的专栏 518

174. Dungeon Game Leetcode Python

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 p

hyperbolechi的专栏 1997

LeetCode 174. Dungeon Game

只能向右或者向下,典型的DP问题。本题问的是能到达右下角的最小初始生命值。 dp[i][j] 表示从 dungeon[i][j] 位置到右下角的最小初始生命值。 dp[i][j] = min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j],同时要保证dp[i][j]始终>0。 为了整合边界条件,dp数组多了一行一列。 本题的关键是和以往的题目不同,子问...

weixin_30755709的博客 87
上一篇: leetcode 152. Maximum Product Subarray
下一篇: leetcode 221. Maximal Square
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值