##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1301.html

# 1301. Number of Paths with Max Score (Hard)

You are given a square board of characters. You can move on the board starting at the bottom right square marked with the character 'S'.

You need to reach the top left square marked with the character 'E'. The rest of the squares are labeled either with a numeric character 1, 2, ..., 9 or with an obstacle 'X'. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.

Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum, taken modulo 10^9 + 7.

In case there is no path, return [0, 0].

Example 1:

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


Example 2:

Input: board = ["E12","1X1","21S"]
Output: [4,2]


Example 3:

Input: board = ["E11","XXX","11S"]
Output: [0,0]


Constraints:

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

Related Topics:
Dynamic Programming

## Solution 1. DP

// OJ: https://leetcode.com/problems/number-of-paths-with-max-score/
// Time: O(MN)
// Space: O(N)
class Solution {
const int mod = 1e9 + 7;
public:
vector<int> pathsWithMaxScore(vector<string>& A) {
int M = A.size(), N = A[0].size();
vector<int> zero(2, 0);
vector<vector<vector<int>>> dp(2, vector<vector<int>>(N, vector<int>(2, 0)));
dp[(M - 1) % 2][N - 1][1] = 1;
for (int i = M - 1; i >= 0; --i) {
for (int j = N - 1; j >= 0; --j) {
if (A[i][j] == 'S') dp[i % 2][j] = { 0, 1 };
else if (A[i][j] == 'X') dp[i % 2][j] = zero;
else {
int sum = 0, cnt = 0;
auto &B = i + 1 < M ? dp[(i + 1) % 2][j] : zero;
auto &R = j + 1 < N ? dp[i % 2][j + 1] : zero;
auto &BR = i + 1 < M && j + 1 < N ? dp[(i + 1) % 2][j + 1] : zero;
sum = max({ B[0], R[0], BR[0] });
if (B[0] == sum) cnt = (cnt + B[1]) % mod;
if (R[0] == sum) cnt = (cnt + R[1]) % mod;
if (BR[0] == sum) cnt = (cnt + BR[1]) % mod;
dp[i % 2][j] = { sum, cnt };
}
if (dp[i % 2][j][1] && isdigit(A[i][j])) dp[i % 2][j][0] += A[i][j] - '0';
}
}
return dp[0][0];
}
};

• class Solution {
public int[] pathsWithMaxScore(List<String> board) {
final int MODULO = 1000000007;
int side = board.size();
char[][] board2D = new char[side][side];
for (int i = 0; i < side; i++) {
String row = board.get(i);
for (int j = 0; j < side; j++)
board2D[i][j] = row.charAt(j);
}
int[][][] dp = new int[side][side][2];
dp[side - 1][side - 1][0] = 0;
dp[side - 1][side - 1][1] = 1;
for (int i = side - 2; i >= 0; i--) {
char cell = board2D[side - 1][i];
if (cell == 'X')
break;
else {
dp[side - 1][i][0] = dp[side - 1][i + 1][0] + cell - '0';
dp[side - 1][i][1] = 1;
}
}
for (int i = side - 2; i >= 0; i--) {
char cell = board2D[i][side - 1];
if (cell == 'X')
break;
else {
dp[i][side - 1][0] = dp[i + 1][side - 1][0] + cell - '0';
dp[i][side - 1][1] = 1;
}
}
for (int i = side - 2; i >= 0; i--) {
for (int j = side - 2; j >= 0; j--) {
char curCell = board2D[i][j];
if (curCell == 'X')
continue;
char downRight = board2D[i + 1][j + 1];
int curScore = curCell == 'E' ? 0 : curCell - '0';
int downScore = dp[i + 1][j][0];
int rightScore = dp[i][j + 1][0];
int downRightScore = dp[i + 1][j + 1][0];
if (downScore > 0 && rightScore > 0) {
if (downScore > rightScore) {
dp[i][j][0] = downScore + curScore;
dp[i][j][1] = dp[i + 1][j][1];
} else if (downScore < rightScore) {
dp[i][j][0] = rightScore + curScore;
dp[i][j][1] = dp[i][j + 1][1];
} else {
if (downScore > 0) {
dp[i][j][0] = downScore + curScore;
dp[i][j][1] = (dp[i + 1][j][1] + dp[i][j + 1][1]) % MODULO;
}
}
} else if (downScore > 0) {
dp[i][j][0] = downScore + curScore;
dp[i][j][1] = dp[i + 1][j][1];
} else if (rightScore > 0) {
dp[i][j][0] = rightScore + curScore;
dp[i][j][1] = dp[i][j + 1][1];
} else {
if (downRightScore > 0 || downRight == 'S') {
dp[i][j][0] = downRightScore + curScore;
dp[i][j][1] = dp[i + 1][j + 1][1];
}
}
}
}
return dp[0][0];
}
}

############

class Solution {
private List<String> board;
private int n;
private int[][] f;
private int[][] g;
private final int mod = (int) 1e9 + 7;

public int[] pathsWithMaxScore(List<String> board) {
n = board.size();
this.board = board;
f = new int[n][n];
g = new int[n][n];
for (var e : f) {
Arrays.fill(e, -1);
}
f[n - 1][n - 1] = 0;
g[n - 1][n - 1] = 1;
for (int i = n - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
update(i, j, i + 1, j);
update(i, j, i, j + 1);
update(i, j, i + 1, j + 1);
if (f[i][j] != -1) {
char c = board.get(i).charAt(j);
if (c >= '0' && c <= '9') {
f[i][j] += (c - '0');
}
}
}
}
int[] ans = new int[2];
if (f[0][0] != -1) {
ans[0] = f[0][0];
ans[1] = g[0][0];
}
return ans;
}

private void update(int i, int j, int x, int y) {
if (x >= n || y >= n || f[x][y] == -1 || board.get(i).charAt(j) == 'X' || board.get(i).charAt(j) == 'S') {
return;
}
if (f[x][y] > f[i][j]) {
f[i][j] = f[x][y];
g[i][j] = g[x][y];
} else if (f[x][y] == f[i][j]) {
g[i][j] = (g[i][j] + g[x][y]) % mod;
}
}
}

• // OJ: https://leetcode.com/problems/number-of-paths-with-max-score/
// Time: O(MN)
// Space: O(N)
class Solution {
const int mod = 1e9 + 7;
public:
vector<int> pathsWithMaxScore(vector<string>& A) {
int M = A.size(), N = A[0].size();
vector<int> zero(2, 0);
vector<vector<vector<int>>> dp(2, vector<vector<int>>(N, vector<int>(2, 0)));
dp[(M - 1) % 2][N - 1][1] = 1;
for (int i = M - 1; i >= 0; --i) {
for (int j = N - 1; j >= 0; --j) {
if (A[i][j] == 'S') dp[i % 2][j] = { 0, 1 };
else if (A[i][j] == 'X') dp[i % 2][j] = zero;
else {
int sum = 0, cnt = 0;
auto &B = i + 1 < M ? dp[(i + 1) % 2][j] : zero;
auto &R = j + 1 < N ? dp[i % 2][j + 1] : zero;
auto &BR = i + 1 < M && j + 1 < N ? dp[(i + 1) % 2][j + 1] : zero;
sum = max({ B[0], R[0], BR[0] });
if (B[0] == sum) cnt = (cnt + B[1]) % mod;
if (R[0] == sum) cnt = (cnt + R[1]) % mod;
if (BR[0] == sum) cnt = (cnt + BR[1]) % mod;
dp[i % 2][j] = { sum, cnt };
}
if (dp[i % 2][j][1] && isdigit(A[i][j])) dp[i % 2][j][0] += A[i][j] - '0';
}
}
return dp[0][0];
}
};

• 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]


• func pathsWithMaxScore(board []string) []int {
n := len(board)
f := make([][]int, n)
g := make([][]int, n)
for i := range f {
f[i] = make([]int, n)
g[i] = make([]int, n)
for j := range f[i] {
f[i][j] = -1
}
}
f[n-1][n-1] = 0
g[n-1][n-1] = 1
const mod = 1e9 + 7

update := func(i, j, x, y int) {
if x >= n || y >= n || f[x][y] == -1 || board[i][j] == 'X' || board[i][j] == 'S' {
return
}
if f[x][y] > f[i][j] {
f[i][j] = f[x][y]
g[i][j] = g[x][y]
} else if f[x][y] == f[i][j] {
g[i][j] = (g[i][j] + g[x][y]) % mod
}
}
for i := n - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
update(i, j, i+1, j)
update(i, j, i, j+1)
update(i, j, i+1, j+1)
if f[i][j] != -1 && board[i][j] >= '0' && board[i][j] <= '9' {
f[i][j] += int(board[i][j] - '0')
}
}
}
ans := make([]int, 2)
if f[0][0] != -1 {
ans[0], ans[1] = f[0][0], g[0][0]
}
return ans
}