[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 (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)-33
-5-101
1030-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:

can use 2d - DP , use matrix to store the value, each element means minimum health to get to bottom right.

Also can simply to 1d array

public class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        if(dungeon.length<=0 || dungeon[0].length<=0) return 1;
        int row = dungeon.length;
        int col = dungeon[0].length;
        int[][] ans = new int[row][col];
        ans[row-1][col-1] = (dungeon[row-1][col-1] >=0) ? 0: Math.abs(dungeon[row-1][col-1]);
        for(int i = col-2;i>=0;i--) {
            ans[row-1][i] = (dungeon[row-1][i]>=ans[row-1][i+1]) ? 0: (ans[row-1][i+1]-dungeon[row-1][i]);
        }
        for(int j = row-2;j>=0;j--) {
            ans[j][col-1] = (dungeon[j][col-1]>=ans[j+1][col-1]) ? 0: (ans[j+1][col-1]-dungeon[j][col-1]);
        }
        for(int i = row-2;i>=0;i--) {
            for(int j = col-2;j>=0;j--) {
                int t1 = (dungeon[i][j]>=ans[i+1][j]) ? 0: (ans[i+1][j]-dungeon[i][j]);
                int t2 = (dungeon[i][j]>=ans[i][j+1]) ? 0: (ans[i][j+1]-dungeon[i][j]);
                ans[i][j] = Math.min(t1, t2);
            }
        }
        return ans[0][0]+1;
    }
}



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 阅读详情

相关推荐

STM32F103C8T6实战:手把手教你搭建智能火灾报警系统(附MQ-2传感器调试技巧)

本文详细介绍了基于STM32F103C8T6单片机和MQ-2烟雾传感器的智能火灾报警系统搭建过程。从硬件选型、电路设计到软件开发,提供全面的实战指导,特别包含MQ-2传感器调试技巧,帮助开发者快速实现稳定可靠的智能火灾监控系统。

year5的博客 213

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] =

支锦铭的博客 339

CTF逆向-[GWCTF 2019]babyvm-WP-虚机模拟流程反向编码和z3约束求解器解方程工具的使用

CTF逆向-[GWCTF 2019]babyvm-WP-虚机模拟流程反向编码和z3约束求解器解方程工具的使用 来源:https://buuoj.cn/ 内容: 附件:https://pan.baidu.com/s/1FKkXN6JDI0QkGw-UE2crNA?pwd=grb0 提取码:grb0 答案:Y0u_hav3_r3v3rs3_1t! 总体思路 发现是虚机模拟 形成每个指令的逆向 找到加密值位置(有可能不止一个位置,该题就给了一个误导的) 找到命令执行的流程(也是不止一个) 逆向编写反向加密得到f

serfend's blog 977

动态规划+使用数组

1. Dungeon Game 题目要求 地牢由M x N房间在2维网格。我们英勇的骑士(K)最初定位在左上的房间,要到达地牢右下角拯救公主。骑士有一个初始健康点(一个正整数)。如果在任何时候或低于他的健康点下降到0,他立即死亡。一些房间有恶魔守卫,所以骑士失去健康(负整数)进入这些房间,其他房间都是空的(0)或含有神奇的魔法球,增加骑士的健康(正整数)。 编写一个函数来确定骑士能够拯

liumengyan_ysu的专栏 643

Windows下Codex CLI binary报错排查与修复完整指南

在Windows开发环境中,命令行工具与图形界面应用的协同工作常常依赖于系统环境变量的正确配置。Codex CLI作为一款基于Node.js的智能编程命令行工具,其可执行文件(binary)的定位机制直接关系到桌面应用的正常启动。当用户执行npm更新或切换Node版本后,Codex CLI二进制文件路径可能发生漂移,从而触发“Unable to locate the Codex CLI binary”的报错。此类问题并非代码或账号异常,而是PATH路径、环境变量刷新及桌面应用缓存共同作用的结果。本文面向Wi

weixin_28718769的博客 305

Windows Server安装Intel I219-V网卡驱动完整教程

在Windows Server系统上安装Intel I219-V网卡驱动的方法。

小白成长历程 5172

174. Dungeon Game

一道动态规划。这道题的难点在于要保证整个过程的血量都要≥1(即使后来的血加回来了大于0也不行,走到一半就死了),又需要是最小的血量,包括初始房间和最后一个房间。 1.到达最后一个房间时要被-5,所以到达时的最小值应该是6,即1-d[2][2],但是如果这个房间是加血的(≥0),会算出一个负数来是不对的,因为从上个房间来的话最小值至少是1。 2.对于下边界和右边界,只能朝一个方向走,所以先处理。现...

MuseCP@YenTown 504

BEV和OCC学习-4:数据准备生成pkl文件(NuSences)

info[‘cam_instances’][‘CAM_XXX’][i][‘bbox_3d’]:长度为 7 的列表,以 (x, y, z, l, h, w, yaw) 的顺序表示实例的 3D 边界框。info[‘cam_instances’][‘CAM_XXX’][i][‘bbox’]:2D 边界框标注(3D 框投影的矩形框),顺序为 [x1, y1, x2, y2] 的列表。info[‘cam_instances’][‘CAM_XXX’][i][‘attr_label’]:实例的属性标签。

zfjBIT的专栏 1699

leetcode:174. 地下城游戏

在上图中,我们知道应该选取绿色路径,因为蓝色路径的路径和太小,使得蓝色路径需要增大初始值到 4才能走到终点,而绿色路径只要 3 点初始值就可以直接走到终点。但是如果我们把终点的-2换成0,蓝色路径只需要初始值2,蓝色路径仍需要初始值3,最优决策就变成蓝色路径了。因此,如果我们按照从左上到右下的顺序进行动态规划,我们就无法直接确定到达 (1,2)的方案,因为有两个重要程度相同的参数同时影响后继的决策。我们希望[从出发点到当前点的路径和]尽可能大,而[从出发点到当前点所需的最小初始值]尽可能小。...

OceanStar的博客 308

解码器选型参数,结构原理,工艺与注意问题总结

解码器芯片(Decoder Chip)是电子设备中的一个重要组件,通常用于处理、解释和输出存储在某种编码格式中的数据或信号。本文对解码器的工作原理,结构特点,工艺流程,选型参数及设计注意事项进行总结。

fydar的博客 1170

OpenCV-Python实战(1)——OpenCV简介与图像处理基础

OpenCV 是一个的跨平台计算机视觉库,可以运行在 Linux、Windows 和 Mac OS 操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时也提供了 Python 接口,实现了图像处理和计算机视觉方面的很多通用算法。在本文中,将介绍 OpenCV 库,包括它的主要模块和典型应用场景,同时使用 OpenCV-Python 实战讲解图像处理基础要点。

盼小辉丶的博客 27万+

[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

把 SAP S/4HANA 中的 Maintain Email Templates 用好:从模板维护、语言变体、业务绑定到传输治理的完整实践

摘要:SAP邮件模板管理常被低估,实则关乎审批通知、业务提醒等关键输出。官方推荐通过复制标准模板实现自定义,而非直接修改标准内容,以保持核心稳定。模板维护涉及语言变体、业务绑定、数据传输等治理环节,需业务顾问与开发团队协作确保字段来源与渲染逻辑。实施时需注意Fiori配置、权限分配及传输管理,避免测试与生产环境差异。模板设计应平衡灵活性与复杂度,遵循分层测试策略(内容、数据、流程),并建立命名规范以维护资产清晰度。(149字)

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 67

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

Camera2Demo

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【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

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

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

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

LeetCode174—Dungeon Game

LeetCode174 Dungeon Game

上善若水 582

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的专栏 517
上一篇: [LeetCode 188] Best Time to Buy and Sell Stock IV
下一篇: [LeetCode 214] Shortest Palindrome
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值