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 == board[i].length <= 100
class Solution {
    public int[] pathsWithMaxScore(List<String> board) {
        int MOD = (int)(1e9 + 7);
        int m = board.size();
        int[][] dp = new int[m+1][m+1];
        int[][] cc = new int[m+1][m+1];
        cc[m-1][m-1] = 1;
        for(int i = m-1;i>=0;i--)
            for(int j = m-1; j>=0;j--){
                char c= board.get(i).charAt(j);
                if(c!=‘X‘){
                    int max = Math.max(Math.max(dp[i+1][j], dp[i][j+1]), dp[i+1][j+1]);
                    int num = c-‘0‘;
                    if(c==‘S‘ || c==‘E‘) num = 0;
                    dp[i][j] = num + max;
                    if(dp[i+1][j] == max) cc[i][j] = (cc[i][j] + cc[i+1][j]) % MOD;
                    if(dp[i+1][j+1] == max) cc[i][j] = (cc[i][j] + cc[i+1][j+1]) % MOD;
                    if(dp[i][j+1] == max) cc[i][j] = (cc[i][j] + cc[i][j+1]) % MOD;
                }
            }

        return cc[0][0] > 0 ? new int[]{dp[0][0], cc[0][0]} : new int[]{0, 0};
    }
}

如果是单纯的最大值,从右下角往上走,每一步选取最大的邻居加上当前值就可以

加上要算path后,当前path必须先判断要从哪个邻居过来(通过判断最大邻居获得)

https://www.youtube.com/watch?v=WwdjLkWmDPs

这题好叼,两个不同的dp相辅相成。注意最后的判断条件是cc[0][0]>0而不能是dp[0][0] > 0, 否则如果中间有一行xxx的话会出现判断错误

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12344867.html

时间: 2024-08-29 09:28:57

1301. Number of Paths with Max Score的相关文章

【leetcode】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 ei

SGU 407 Number of Paths in the Empire dp+java大数

SGU 407 407. Number of Paths in the Empire Time limit per test: 0.75 second(s) Memory limit: 65536 kilobytes input: standard output: standard During the period of Tsam dynasty ruling people rarely fought against each other and their neighbours, becau

200. Number of Islands + 695. Max Area of Island

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

3ds Max File Format (Part 1: The outer file format; OLE2)

The 3ds Max file format, not too much documentation to be found about it. There are some hints here and there about how it’s built up, but there exists no central documentation on it. Right now we are in the following situation. A few thousand of max

Jan 19 - Unique Paths; Array; DP;

first of all, we're goin to create a 2D array(int[][] grid) to store the number of paths to each position. we get the minimum value of m and n, mark it as min, then look through the row and column of grid[i][i] in each loop, when i == 0, we know we a

ExtJS学习-----------Ext.Number,ExtJS对javascript中的Number的扩展

关于ExtJS对javascript中的Number的扩展,可以参考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 下面对其中的部分方法进行介绍: (1)constrain constrain( Number number, Number min, Number max ) : Number 检查给定的数值是否在约束的范围内. If the number is already within the 如果再范围内就返

[Leetcode][JAVA] Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 对每个点,考察其他点与它组成的直线斜率,使用HashMap将斜率与点个数对应起来. 需要注意的一点是特殊斜率,比如两点重复,两点横坐标相同.所以用String表示斜率比较好 获取斜率函数: 1 public String getSlope(Point p1, Point p2) 2 { 3 if(p

Hackerrank--Lexicographic paths

题目链接 Krishnakant is standing at (0,0) in the Cartesian plane. He wants to go to the point (N,M) in the same plane using only horizontal and vertical moves of 1 unit. There are many ways of doing this, and he is writing down all such ways. Every way w

576. Out of Boundary Paths

There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However, you can at most move N times. Find out the numbe