地址 https://leetcode-cn.com/problems/number-of-paths-with-max-score/

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

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

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

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

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

示例 :

输入:board = ["E23","2X2","12S"]
输出:[,]
示例 : 输入:board = ["E12","1X1","21S"]
输出:[,]
示例 : 输入:board = ["E11","XXX","11S"]
输出:[,]
  提示: <= board.length == board[i].length <=

解答

使用动态规划  因为走到当前的格子肯定是当前格子的右方 下方和右下方。 那么他的右下方 右方和下方的状态中 那些是得分最高的就决定了当前的得分和走法方案数

代码如下 注意DP有额外多开一层 为了保持代码的一致性。

代码起点dp判断如下: n = board.size() ;  dp[n-1][n-1] =  max(max(dp[n][n-1], dp[n-1][n]), dp[n][n]);

由于dp初始均为0  所以不影响起点 dp[n-1][n-1]的取值

class Solution {
public:
vector<vector<int>> dp;
vector<vector<int>> path; vector<int> pathsWithMaxScore(vector<string>& board) {
const int n = board.size();
const int MOD = 1e9+;
dp.resize(n+, vector<int>(n+));
path.resize(n+,vector<int>(n+)); board[n - ][n - ] = '';
board[][] = ''; path[n - ][n - ] = ; for (int i = n - ; i >= ; i--) {
for (int j = n - ; j >= ; j--) {
if (board[i][j] == 'X') continue;
int m = max(max(dp[i + ][j], dp[i][j + ]), dp[i + ][j + ]);
dp[i][j] = ( (board[i][j] - '') + m ) %MOD; if (dp[i + ][j] == m) path[i][j] = ( path[i][j]+path[i+][j])%MOD;
if (dp[i + ][j+] == m) path[i][j] = (path[i][j] + path[i + ][j+]) %MOD ;
if (dp[i][j+] == m) path[i][j] = ( path[i][j]+ path[i][j+])%MOD;
}
}
if(path[][] == ) return vector<int>({,});
return vector<int>({dp[][],path[][]});
} };

leetcode 1301. 最大得分的路径数目的更多相关文章

  1. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  2. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  3. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  4. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  5. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. [LeetCode] Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. PAT甲题题解-1003. Emergency (25)-最短路径+路径数目

    给出n个城市,m条边,起始点c1和目的点c2接下来给出n个城市的队伍数以及m条双向边问你求c1到c2的所有最短路径数目,以及其中经过的最多队伍数 先最短路dijkstra,同时建立vector数组pr ...

  9. LeetCode刷题笔记-递归-路径总和

    题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 su ...

随机推荐

  1. SELinux 宽容模式(permissive) 强制模式(enforcing) 关闭(disabled) 几种模式之间的转换

    http://blog.sina.com.cn/s/blog_5aee9eaf0100y44q.html 在CentOS6.2 中安装intel 的c++和fortran 的编译器时,遇到来一个关于S ...

  2. oracle RANK() dense_rank()

    [语法]RANK ( ) OVER ( [query_partition_clause] order_by_clause ) dense_RANK ( ) OVER ( [query_partitio ...

  3. oracle函数 INSTR(C1,C2[,I[,J]])

    [功能]在一个字符串中搜索指定的字符,返回发现指定的字符的位置; [说明]多字节符(汉字.全角符等),按1个字符计算 [参数] C1    被搜索的字符串 C2    希望搜索的字符串 I     搜 ...

  4. 网上很多laravel中cookie的使用方法。

    https://blog.csdn.net/chen529834149/article/details/75244718 概述 Cookie的添加其实很简单,直接使用Cookie::make(),在使 ...

  5. phpmyadmin设置自动登录和取消自动登录

      1首先在phpmyadmin安装目录下找到config.sample.inc.php复制一份文件名改为config.inc.php 2打开config.inc.php 找到 $cfg['Serve ...

  6. H3C 配置RIP peer

  7. springmvc 过滤器和拦截器

     1. 拦截器: interceptor 过滤器(filter)与拦截器(intercepter)相同点:1) 都可以拦截请求,过滤请求2) 都是应用了过滤器(责任链)设计模式 2.区别: 1) fi ...

  8. Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage)

    解决方案是删除 pom.xml配置的问题 <build> <plugins> <plugin> <groupId>org.springframework ...

  9. H3C ACL包过滤配置任务

  10. 51nod1327 棋盘游戏

    远古大坑 神仙DP状态设计题 https://blog.csdn.net/white_elephant/article/details/83592103 从行的角度入手,无论如何都要状压 每列最多放一 ...