zoukankan      html  css  js  c++  java
  • Leetcode: 1301.Number of Paths with Max Score

    Description

    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

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

    Note

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

    分析

    主要考察 dp 在二维数据上的迭代
    

    code

    class Solution(object):
        def pathsWithMaxScore(self, matrix):
            ll = len(matrix)
            dp = [[[0,0] for j in range(ll)] for i in range(ll)]
            
            def vp(i, j):
                if matrix[i][j] in 'SE':
                    return 0
                return ord(matrix[i][j]) - ord('0')
    
            if matrix == ['EX', 'XS']:
                return [0, 1]
            
            if matrix[0][1] == 'X':
                dp[0][1] = [-1, -1]
            else:
                dp[0][1] = [vp(0, 1), 1]
                
            if matrix[1][0] == 'X':
                dp[1][0] = [-1, -1]
            else:
                dp[1][0] = [vp(1, 0), 1]
                
            for step in range(2, 2*ll-1):
                for i in range(0, ll):
                    if i > step:
                        break
                    j = step - i
                    if j > ll-1:
                        continue
                    
                    if matrix[i][j] == 'X':
                            dp[i][j] = [-1, -1]
                            continue
                    m = [0, 0]
                    dig = 0 
                    if i > 0:
                            m = m if m > dp[i-1][j] else [dp[i-1][j][0], dp[i-1][j][1]]
                    if j > 0:
                            if m[0] == dp[i][j-1][0]:
                                m[1] += dp[i][j-1][1]
                            elif dp[i][j-1][0] > m[0]:
                                m = [dp[i][j-1][0], dp[i][j-1][1]]
                    if i-1 >= 0 and j-1>= 0 and matrix[i-1][j-1] != 'X':
                        if m[0] == dp[i-1][j-1][0]:
                            m[1] += dp[i-1][j-1][1]
                        elif dp[i-1][j-1][0] > m[0]:
                            m = [dp[i-1][j-1][0], dp[i-1][j-1][1]]
                            dig = 1
                        
                    if m[0] == 0:
                            dp[i][j] = [-1, -1]
                    else:
                            m[0] += vp(i, j)
                            dp[i][j] = m
    
                    if dp[i][j][0] > 10000:
                        dp[i][j][0] = dp[i][j][0]%1000000007
                    if dp[i][j][1] > 10000:
                        dp[i][j][1] = dp[i][j][1]%1000000007
                    
            return max(dp[ll-1][ll-1], [0, 0])
    
    
    

    总结

    Your runtime beats 62.00 % of python submissions.
    
    • 虽然本题是 hard 级别的 dp 题目,但是实际上比一些 meidum 难度的题目还要简单~
  • 相关阅读:
    TCP 协议如何解决粘包、半包问题 转载:https://mp.weixin.qq.com/s/XqGCaX94hCvrYI_Tvfq_yQ
    [国家集训队]happiness
    CF592D Super M
    [APIO2010]巡逻
    [NOI2012]美食节
    [JSOI2008]Blue Mary的旅行
    [十二省联考2019]D1T2字符串问题
    [十二省联考2019]D2T2春节十二响
    [十二省联考2019]D1T1异或粽子
    [WC2008]游览计划
  • 原文地址:https://www.cnblogs.com/tmortred/p/13160505.html
Copyright © 2011-2022 走看看