[email protected] [174] Dungeon Game (Dynamic Programming)

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 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:

1.The knight‘s health has no upper bound.

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

class Solution {
public:
    int calculateMinimumHP(vector<vector<int>>& dungeon) {
        if(dungeon.size() == 0) return 1;

        int m = dungeon.size(), n = dungeon[0].size();
        vector<vector<int> > dp(m, vector<int>(n, 0));

        dp[m-1][n-1] = (1-dungeon[m-1][n-1]<=0)? 1: 1-dungeon[m-1][n-1];

        for(int j=n-2;j>=0;--j) {
            dp[m-1][j] = (dp[m-1][j+1]-dungeon[m-1][j] <= 0)? 1: dp[m-1][j+1]-dungeon[m-1][j];
        }
        for(int i=m-2;i>=0;--i) {
            dp[i][n-1] = (dp[i+1][n-1]-dungeon[i][n-1] <= 0)? 1: dp[i+1][n-1]-dungeon[i][n-1];
        }

        for(int i=m-2;i>=0;--i) {
            for(int j=n-2;j>=0;--j) {
                bool flag = (dp[i][j+1]<=dungeon[i][j] || dp[i+1][j]<=dungeon[i][j]);
                dp[i][j] = flag? 1: min(dp[i][j+1]-dungeon[i][j], dp[i+1][j]-dungeon[i][j]);
            }
        }

        return dp[0][0];
    }
};

时间: 2024-10-12 01:04:06

[email protected] [174] Dungeon Game (Dynamic Programming)的相关文章

[email&#160;protected] [91] Decode Ways (Dynamic Programming)

https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of wa

[email&#160;protected] Minimum sum partition (Dynamic Programming)

http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum. Input: The first line contains an i

[email&#160;protected] [322] Coin Change (Dynamic Programming)

https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot

[email&#160;protected] [134] Gas station (Dynamic Programming)

https://leetcode.com/problems/gas-station/ There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next sta

hdu 4223 Dynamic Programming?

Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solving complex problems by breaking them down

2016 UESTC Training for Dynamic Programming

2016 UESTC Training for Dynamic Programming A - 柱爷与咸鱼神功 题意: 柱爷有n(<=5000)点心情去学m(<=5000)个招式,每个招式会得到一定的修炼值,但要消耗一定的心情,求最多的修炼值. 题解: 0.这是一个裸的背包问题,用每一个物品去更新每一种背包的状态. 1.状态定义:dp[i]表示用i点心情得到的最多修炼值. 2.状态转移:dp[i] = max{dp[i-v[j]]+w[j]} 代码: 1 2 3 4 5 6 7 8 9 10

Dynamic Programming 动态规划

Dynamic Programming DP 的历史渊源: Richard E. Bellman (1920-1984) Richard Bellman received the IEEE Medal of Honor, 1979. "Bellman . . . explained that he invented the name 'dynamid programming' to hide the fact that he was doing mathe-matical research at

【[email&#160;protected]基础篇 ~】# 磁盘与文件系统

之前三篇文章我们简单介绍了Linux系统的用户管理,文件操作等,都是比较浅显的基本操作.这节我们要深入一下了,从文件系统我们要看到磁盘系统.从磁盘系统我们要看到操作系统的整体架构.废话不多少让我们开始学习吧! 磁盘与文件系统 1.磁盘系统 1.1 磁盘结构 如图所示,磁盘由扇区和柱面组成,分区的最小单位是柱面(柱是有厚度的,本图是截面图),磁盘读取的最小单位是扇区.第一扇区的MBR(446bytes)分区表可以最大包含四个分区(64bytes)的信息,即从开始柱面到结束柱面4组数据,每组16个字

IBATIS动态SQL--&lt;dynamic&gt;[email&#160;protected]@Identity

一.<dynamic><isNotNull> <!-- select 基本语句 --> <select id="tbhyScenarioSetting.select" parameterClass="tbhyScenarioSettingDomain" resultClass="tbhyScenarioSettingDomain"> select id as id, gmt_create as gm