AlgoFrames

Dungeon game

Challenge · Write code · about 25 min

A knight starts in the top-left room of dungeon and must reach the princess in the bottom-right room, moving only right or down. Entering a room changes the knight's health by its value: negative values hurt, positive values heal. The start and end rooms count too.

If health ever drops to 0 or below, the knight dies. Return the smallest starting health that lets the knight reach the princess.

Example 1

Input:

[[[-2,-3,3],[-5,-10,1],[10,30,-5]]]

Output:

7

Example 2

Input:

[[[0]]]

Output:

1

Health must stay at least 1.

Constraints

  • 1 ≤ rows, cols ≤ 200
  • -100 ≤ dungeon[r][c] ≤ 100

Learn the idea first: Grid DP: paths, minimum path sum, obstacles, a visual lesson.

More Grid DP: paths, minimum path sum, obstacles problems