地址 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. el-table中加载图片问题

    <el-table-column label="头像" width="100"> <template scope="scope&qu ...

  2. TextView.setTextColor(int); 括号里那个颜色int值的理解

    原本以为是R.id里的东西,后来发现不是 http://dianhua1990627.blog.163.com/blog/static/2755558820132262150387/

  3. ROS开发过程中遇到:Could not find a package configuration file provided by "qt_build" with any of the following names: qt_buildConfig.cmake qt_build-config.cmake........

    最近在搭建QT开发ROS 界面的环境,遇到了很多问题,参考了很多资料,最后发现有些问题其实没有那么复杂,只是我们对整体环境还不了解,熟悉了以后你会发现有些问题就迎刃而解了. 在这个过程中,我首先新建了 ...

  4. Knative 初体验:Eventing Hello World

    作者 | 阿里云智能事业群高级开发工程师 元毅 基于事件驱动是Serveless的核心功能之一,通过事件驱动服务,满足了用户按需付费(Pay-as-you-go)的需求.在之前的文章中我们介绍过 Kn ...

  5. oracle函数 NLS_LOWER(x[,y])

    [功能]返回字符串并将字符串的变为小写; [参数]x字符型表达式 [参数]Nls_param可选,指定排序的方式(nls_sort=) . SCHINESE_RADICAL_M(部首.笔画) SCHI ...

  6. Export与import命令

    模块功能主要由两个命令构成:export和import. export命令用于用户自定义模块,规定对外接口: import命令用于输入其他模块提供的功能,同时创造命名空间(namespace),防止函 ...

  7. HDU-6703-array-2019CCPC选拔赛

    我TM真是一个弟弟... 题意: 给出一串1-N的数字 你每次可以把某个位置的值+1000000 或者找一个值,所有a[1]...a[r]序列的数都不能等于这个值,并且这个值>w 当时比赛觉得肯 ...

  8. H3C 主机接收IP包

  9. Jieba分词原理与解析

    https://www.jianshu.com/p/dfdfeaa7d01f 1 HMM模型   image.png 马尔科夫过程:   image.png   image.png 以天气判断为例:引 ...

  10. SQL 三个表练习(student,teacher,score)