package temworkingarea;
/**
* <pre>
* 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.
* </pre>
* */
public class DungeonGame {
public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int row = dungeon.length;
int column = dungeon[0].length;
int[][] tem = new int[row][];
for (int i = 0; i < tem.length; i++) {
tem[i] = new int[column];
}
if (dungeon[row - 1][column - 1] >= 0) {
tem[row - 1][column - 1] = 1;
} else {
tem[row - 1][column - 1] = 1 - dungeon[row - 1][column - 1];
}
for (int i = row - 2; i >= 0; i--) {
tem[i][column - 1] = c(dungeon[i][column - 1],
tem[i + 1][column - 1]);
}
for (int j = column - 2; j >= 0; j--) {
tem[row - 1][j] = c(dungeon[row - 1][j], tem[row - 1][j + 1]);
}
for (int i = row - 2; i >= 0; i--) {
for (int j = column - 2; j >= 0; j--) {
tem[i][j] = Math.min(c(dungeon[i][j], tem[i + 1][j]),
c(dungeon[i][j], tem[i][j + 1]));
}
}
return tem[0][0];
}
private int c(int value, int preResult) {
if (value == 0)
return preResult;
if (value > 0) {
if (value >= preResult)
return 1;
return preResult - value;
}
return preResult - value;
}
}
}
分享到:
相关推荐
java java_leetcode题解之Game of Life.java
《使用leetcode-editor在IDE中进行LeetCode练习的全方位指南》 LeetCode是一个广受欢迎的在线编程练习平台,它提供了一系列的算法题目供程序员们提升技能。对于习惯在集成开发环境(IDE)中工作的开发者来说,将...
vs code LeetCode 插件
《Python版LeetCode题解全集详解》 LeetCode是一个广受欢迎的在线编程挑战平台,致力于帮助程序员提升技能,特别是面试准备。这个压缩包“lc-all-solutions-master”包含了使用Python语言解决LeetCode所有问题的...
"IDEA leetcode-editor插件"就是将这两者结合的工具,允许用户在IDEA中直接进行LeetCode的编程挑战,无需离开开发环境,提高了刷题的便捷性。 该插件的主要功能包括: 1. **离线模式**:在无法访问LeetCode官网的...
"candy.erl","dungeon_game.erl", "interleaving_string.erl","search_insert_position.erl", "three_sum.erl","trapping_rain_water.erl", "valid_palindrome.erl" 个人认为dungeon_game这个题目解题逻辑很体现...
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
(C++)LeetCode刷题题解答案
leetcode刷题, 直接用leetcode的分类方式.
基于Python实现的LeetCode爬虫爬取LeetCode题目描述和提交的代码.zip ## 特点 - 支持爬取题目列表,保存为本地CSV/Excel文件。 - 支持爬取题目描述,保存为本地HTML文件。 - 支持爬取用户提交的代码,保存为如_.py...
LeetCode面试笔试题
### LeetCode中文版知识点概述 #### 一、LeetCode简介与使用 LeetCode是一个全球知名的在线编程学习平台,主要提供各种算法题目供开发者练习。它不仅涵盖了基础数据结构和算法训练,还包括了大量的面试真题,是...
LeetCode-Swift, 快速LeetCode解决方案 LeetCodeLeetCode在线判断是一个包含很多收费算法的网站。 them Google Google Google Google LinkedIn this this repo 。 请免费参考并收费STAR以支持这个 repo,
java java_leetcode题解之Baseball Game.java
《LeetCode经典题目全解析》是一份由编程高手精心编撰的文档,旨在全面解析LeetCode平台上众多的算法挑战题目。LeetCode作为一个知名的在线编程练习平台,汇集了各种难度级别的编程题目,涵盖数据结构、算法、设计...
leetcode 习题集, leetcode 官网出品,包含基本的算法
LeetCode 400题目 Java版本 (LeetCode is the platform to help you enhance your skills, expand your knowledge and prepare for technical interviews.)
力扣(LeetCode) 相比其他编程平台有着很多优势: **各大知名公司面试真题:**对于求职者在这上面训练更具有针对性,目前国内一些公司面试时直接从在这上面出题。 **大中小企业都在使用:**常常会直接或者间接...
java java_leetcode题解之Jump Game II.java
《LeetCode Editor 7.4:提升编程技能的利器》 在编程学习和实践中,LeetCode 已经成为了程序员们磨炼算法、提升编程技能的重要平台。为了方便开发者更高效地进行刷题,LeetCode 提供了官方编辑器插件——LeetCode ...