All problems
0847HardArrayDynamic ProgrammingMatrix

Richest Descent Through the Quarry

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1301Number of Paths with Max Score

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A quarry face is a square grid of cells given as board, a list of equal-length strings. The cell in the bottom right corner holds 'S', the start, and the cell in the top left corner holds 'E', the exit. Every other cell holds either a digit '1' through '9', which is the ore it yields, or 'X', which is impassable.

A hauler starts on 'S' and must reach 'E', moving each step to the cell directly above, directly to the left, or diagonally up and to the left. It may never enter an 'X' cell. The haul of a route is the total ore of the cells it enters, counting neither 'S' nor 'E', which yield nothing.

Return a list of two numbers: the largest haul any route can collect, and how many routes collect it, that count taken modulo 1000000007. If no route reaches the exit at all, return [0, 0].

Examples

Example 1

Input
board = ["E23", "2X2", "12S"]
Output
[7, 1]

One route runs from the start up to the 2 above it, then up to the 3, then left twice to the exit, gathering 2 + 3 + 2 = 7. Another gathers the same total by a different set of cells.

Example 2

Input
board = ["EX", "XS"]
Output
[0, 1]

Both cells beside the start are impassable and the diagonal step lands on the exit, so no route reaches it and the answer is [0, 0].

Example 3

Input
board = ["E1", "1S"]
Output
[1, 2]

The hauler can step up then left, or left then up, each gathering the single 1 on the way, or take the diagonal step straight to the exit gathering nothing. The largest haul is 1 and two routes collect it.

Constraints

  • 2 <= board.length <= 100
  • Every string of board has the same length as board.length
  • board[i][j] is one of 'S', 'E', 'X' or a digit from '1' to '9'
  • board[board.length - 1][board.length - 1] is 'S' and board[0][0] is 'E'

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def richest_descent(board: list[str]) -> list[int]:
Java
public int[] richestDescent(List<String> board)
September 7
Apply