leetcode | 174. Dungeon Game

题目

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.

-2 (K)-33
-5-101
1030-5 (P)

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走到右下叫P的路径,使得经此路径骑士所需的最初血量最少。乍一看,这道题目与64. Minimum Path Sum似乎有些类似,都是从地图左上角走到右下角,找到一条最佳路径使得所需代价最少。

错误的思路

直观上,我们会得到以下想法:
对于节点(i,j),当(i-1,j)所需最初血量更少时,选择(i-1,j),否则选择(i,j-1);如果两者相同,则选择(i-1,j)(i,j-1)中当前所剩血量更大的节点。
但是,这道题目不能用以上的思路,节点(i,j)的代价值虽然与(i-1,j)、(i,j-1)两个节点直接相关,但该节点其实也受到后续路径的影响,我们所得到的仅仅是局部最优解而已。以一个例子说明此现象:节点下标范围i ∈[0,rows-1],j∈[0, cols-1]

1K-33
0-20
-3-3-3(P)

对于节点(1,2),其直接代价来源为(0,2)(1,1);此时我们假设骑士最初的血量为0,则从(0,0)移动到(0,2)过程中,有如下表格:

节点(0,0)(0,1)(0,2)
血量1-21
最初所需最小血量022

(0,0)移动到(1,1)过程中,有如下表格:

节点(0,0)(1,0)(1,1)
血量11-1
最初所需最小血量001

右上述两条路径可知,到达(1,1)时所需的最初血量更少,所以,节点(1,2)会选择节点(1,1),此时,到达节点(2,2)所需最初血量为5。但实际上,如果节点(1,2)选择节点(0,2),则到达节点(2,2)所需最初血量为3。所以我们最初的想法并不成立。

正确的思路

由于当前节点的选择收到后续路径的影响,所以我们可以采取由后向前的想法来解决,即从终点向起点进行递推。
定义dp[i][j]表示从节点(i,j)到达终点P所需的最小初始血量。则存在以下状态转移方程:

// 一般节点(并不纳入边界点)
// Right
if dungeon[i][j] > dp[i+1][j] {
	dp[i][j] = 0
} else {
	dp[i][j] = dp[i+1][j] - dungeon[i][j]
}

// Down
if dungeon[i][j] > dp[i][j+1] {
	dp[i][j] = min(0, dp[i][j])
} else {
	dp[i][j] = min(dp[i][j+1]-dungeon[i][j], dp[i][j])
}

最终返回结果为dp[0][0]+1,原因是血量必须大于0。

代码实现

const INT_MIN = ^int(^uint(0) >> 1)
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func calculateMinimumHP(dungeon [][]int) int {
	rows := len(dungeon)
	cols := len(dungeon[0])
	dp := make([][]int, rows)
	for i := 0; i < rows; i++ {
		dp[i] = make([]int, cols)
	}
	// 右下角
	if dungeon[rows-1][cols-1] > 0 {
		dp[rows-1][cols-1] = 0
	} else {
		dp[rows-1][cols-1] = -dungeon[rows-1][cols-1]
	}
	// 边界-右
	for i := rows - 2; i >= 0; i-- {
		if dungeon[i][cols-1] > dp[i+1][cols-1] {
			dp[i][cols-1] = 0
		} else {
			dp[i][cols-1] = dp[i+1][cols-1] - dungeon[i][cols-1]
		}
	}
	// 边界-下
	for j := cols - 2; j >= 0; j-- {
		if dungeon[rows-1][j] > dp[rows-1][j+1] {
			dp[rows-1][j] = 0
		} else {
			dp[rows-1][j] = dp[rows-1][j+1] - dungeon[rows-1][j]
		}
	}
	for i := rows - 2; i >= 0; i-- {
		for j := cols - 2; j >= 0; j-- {
			// Right
			if dungeon[i][j] > dp[i+1][j] {
				dp[i][j] = 0
			} else {
				dp[i][j] = dp[i+1][j] - dungeon[i][j]
			}
			// Down
			if dungeon[i][j] > dp[i][j+1] {
				dp[i][j] = min(0, dp[i][j])
			} else {
				dp[i][j] = min(dp[i][j+1]-dungeon[i][j], dp[i][j])
			}
		}
	}
	return dp[0][0] + 1
}

时间复杂度分析

有上述代码可知,时间复杂度为O(M*N)。运行结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值