LeetCode 题解工作台

最大得分的路径数目

给你一个正方形字符数组 board ,你从数组最右下方的字符 'S' 出发。 你的目标是到达数组最左上角的字符 'E' ,数组剩余的部分为数字字符 1, 2, ..., 9 或者障碍 'X' 。在每一步移动中,你可以向上、向左或者左上方移动,可以移动的前提是到达的格子没有障碍。 一条路径的 「得分」…

category

3

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们定义 表示从起点 $(n - 1, n - 1)$ 到达 $(i, j)$ 的最大得分,定义 表示从起点 $(n - 1, n - 1)$ 到达 $(i, j)$ 的最大得分的方案数。初始时 $f[n - 1][n - 1] = 0$,并且 $g[n - 1][n - 1] = 1$。其它位置的 均为 ,而 均为 。 对于当前位置 $(i, j)$,它可以由 $(i + 1, j)$,…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个正方形字符数组 board ,你从数组最右下方的字符 'S' 出发。

你的目标是到达数组最左上角的字符 'E' ,数组剩余的部分为数字字符 1, 2, ..., 9 或者障碍 'X'。在每一步移动中,你可以向上、向左或者左上方移动,可以移动的前提是到达的格子没有障碍。

一条路径的 「得分」 定义为:路径上所有数字的和。

请你返回一个列表,包含两个整数:第一个整数是 「得分」 的最大值,第二个整数是得到最大得分的方案数,请把结果对 10^9 + 7 取余

如果没有任何路径可以到达终点,请返回 [0, 0]

 

示例 1:

输入:board = ["E23","2X2","12S"]
输出:[7,1]

示例 2:

输入:board = ["E12","1X1","21S"]
输出:[4,2]

示例 3:

输入:board = ["E11","XXX","11S"]
输出:[0,0]

 

提示:

  • 2 <= board.length == board[i].length <= 100
lightbulb

解题思路

方法一:动态规划

我们定义 f[i][j]f[i][j] 表示从起点 (n1,n1)(n - 1, n - 1) 到达 (i,j)(i, j) 的最大得分,定义 g[i][j]g[i][j] 表示从起点 (n1,n1)(n - 1, n - 1) 到达 (i,j)(i, j) 的最大得分的方案数。初始时 f[n1][n1]=0f[n - 1][n - 1] = 0,并且 g[n1][n1]=1g[n - 1][n - 1] = 1。其它位置的 f[i][j]f[i][j] 均为 1-1,而 g[i][j]g[i][j] 均为 00

对于当前位置 (i,j)(i, j),它可以由 (i+1,j)(i + 1, j), (i,j+1)(i, j + 1), (i+1,j+1)(i + 1, j + 1) 三个位置转移而来,因此我们可以枚举这三个位置,更新 f[i][j]f[i][j]g[i][j]g[i][j] 的值。如果当前位置 (i,j)(i, j) 有障碍,或者当前位置是起点,或者其它位置越界,则不进行更新。否则,如果其它位置 (x,y)(x, y) 满足 f[x][y]>f[i][j]f[x][y] \gt f[i][j],那么我们更新 f[i][j]=f[x][y]f[i][j] = f[x][y],并且 g[i][j]=g[x][y]g[i][j] = g[x][y]。如果 f[x][y]=f[i][j]f[x][y] = f[i][j],那么我们更新 g[i][j]=g[i][j]+g[x][y]g[i][j] = g[i][j] + g[x][y]。最后,如果当前位置 (i,j)(i, j) 可达并且是数字,我们更新 f[i][j]=f[i][j]+board[i][j]f[i][j] = f[i][j] + board[i][j]

最后,如果 f[0][0]<0f[0][0] \lt 0,说明没有路径可以到达终点,返回 [0,0][0, 0]。否则,返回 [f[0][0],g[0][0]][f[0][0], g[0][0]]。注意,返回结果需要对 109+710^9 + 7 取余。

时间复杂度 O(n2)O(n^2),空间复杂度 O(n2)O(n^2)。其中 nn 是数组的边长。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def pathsWithMaxScore(self, board: List[str]) -> List[int]:
        def update(i, j, x, y):
            if x >= n or y >= n or f[x][y] == -1 or board[i][j] in "XS":
                return
            if f[x][y] > f[i][j]:
                f[i][j] = f[x][y]
                g[i][j] = g[x][y]
            elif f[x][y] == f[i][j]:
                g[i][j] += g[x][y]

        n = len(board)
        f = [[-1] * n for _ in range(n)]
        g = [[0] * n for _ in range(n)]
        f[-1][-1], g[-1][-1] = 0, 1
        for i in range(n - 1, -1, -1):
            for j in range(n - 1, -1, -1):
                update(i, j, i + 1, j)
                update(i, j, i, j + 1)
                update(i, j, i + 1, j + 1)
                if f[i][j] != -1 and board[i][j].isdigit():
                    f[i][j] += int(board[i][j])
        mod = 10**9 + 7
        return [0, 0] if f[0][0] == -1 else [f[0][0], g[0][0] % mod]
speed

复杂度分析

指标
时间complexity is O(n^2) since each cell is processed once and each has at most three neighbors. Space complexity is O(n^2) for the DP table, but can be reduced to O(n) by storing only the current and previous row.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Can you define a DP state that tracks both sum and count together?

  • question_mark

    How do you handle obstacles in the dynamic programming transition?

  • question_mark

    What edge cases cause path count to be zero despite numeric values on the board?

warning

常见陷阱

外企场景
  • error

    Counting paths from neighbors that do not contribute to max_sum, inflating path counts.

  • error

    Failing to initialize the starting cell or obstacles correctly, causing DP propagation errors.

  • error

    Ignoring the modulo requirement for path counts, leading to integer overflow.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow moves in all four directions, changing the state transition rules.

  • arrow_right_alt

    Compute minimum score paths instead of maximum, requiring adjustment in comparison logic.

  • arrow_right_alt

    Use larger boards with more obstacles, testing space optimization strategies.

help

常见问题

外企场景

最大得分的路径数目题解:状态·转移·动态规划 | LeetCode #1301 困难