LeetCode //C - 1301. Number of Paths with Max Score

1301. Number of Paths with Max Score

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 == boardi.length <= 100

From: LeetCode

Link: 1301. Number of Paths with Max Score


Solution:

Ideas:

Use dynamic programming to store the maximum score and number of maximum-score paths reaching each cell.

Code:
c 复制代码
#include <stdlib.h>
#include <string.h>

#define MOD 1000000007

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* pathsWithMaxScore(char **board, int boardSize, int* returnSize) {
    int maxScore[100][100];
    int pathCount[100][100];

    for (int i = 0; i < boardSize; i++) {
        for (int j = 0; j < boardSize; j++) {
            maxScore[i][j] = -1;
            pathCount[i][j] = 0;
        }
    }

    maxScore[0][0] = 0;
    pathCount[0][0] = 1;

    for (int i = 0; i < boardSize; i++) {
        for (int j = 0; j < boardSize; j++) {
            if (board[i][j] == 'X' || (i == 0 && j == 0)) {
                continue;
            }

            int best = -1;
            long long ways = 0;

            /* Cell above */
            if (i > 0 && maxScore[i - 1][j] > best) {
                best = maxScore[i - 1][j];
                ways = pathCount[i - 1][j];
            } else if (i > 0 && maxScore[i - 1][j] == best && best != -1) {
                ways += pathCount[i - 1][j];
            }

            /* Cell to the left */
            if (j > 0 && maxScore[i][j - 1] > best) {
                best = maxScore[i][j - 1];
                ways = pathCount[i][j - 1];
            } else if (j > 0 && maxScore[i][j - 1] == best && best != -1) {
                ways += pathCount[i][j - 1];
            }

            /* Upper-left diagonal cell */
            if (i > 0 && j > 0 && maxScore[i - 1][j - 1] > best) {
                best = maxScore[i - 1][j - 1];
                ways = pathCount[i - 1][j - 1];
            } else if (i > 0 && j > 0 &&
                       maxScore[i - 1][j - 1] == best && best != -1) {
                ways += pathCount[i - 1][j - 1];
            }

            if (best == -1) {
                continue;
            }

            int value = 0;

            if (board[i][j] >= '1' && board[i][j] <= '9') {
                value = board[i][j] - '0';
            }

            maxScore[i][j] = best + value;
            pathCount[i][j] = ways % MOD;
        }
    }

    int* result = (int*)malloc(2 * sizeof(int));
    *returnSize = 2;

    if (maxScore[boardSize - 1][boardSize - 1] == -1) {
        result[0] = 0;
        result[1] = 0;
    } else {
        result[0] = maxScore[boardSize - 1][boardSize - 1];
        result[1] = pathCount[boardSize - 1][boardSize - 1];
    }

    return result;
}
相关推荐
专注仿真1 小时前
仿真智能体训练(评估篇):基于蒙特卡洛批量对抗的评估算法详解与实战
算法·仿真·评估·afsim智能体·仿真训练
Matt Feng2 小时前
STM32串口ISP多路烧录:4个关键参数与批量下载步骤
c语言·stm32·单片机·嵌入式硬件·接口隔离原则
derek19973 小时前
软考软件设计师:B-树和 B+树的区别、线性探查法查找时到底比较了谁
c语言·c++·计算机网络·软件工程·学习方法
小灰灰搞电子3 小时前
C 语言基础:动态内存管理详解
c语言·开发语言
小O的算法实验室7 小时前
SEVC,城市灾害环境下应急物流的帕累托归档引导式多目标路径规划
算法
维克兜率天7 小时前
【维克】布林带:通道类指标的“量化基石“
人工智能·python·算法
风落青芜7 小时前
C++入门:类和对象(二)
开发语言·数据结构·c++·算法
一只QAQ7 小时前
c语言八股
c语言·开发语言
wuyk5558 小时前
AI 时代的嵌入式该怎么学-11-C不是语法是对内存的显式建模
c语言·开发语言·stm32·单片机·嵌入式硬件·mcu·算法