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 难度的题目还要简单~
  • 相关阅读:
    修理牛棚 贪心 USACO
    零件加工 贪心 题解
    花店橱窗 动态规划 题解
    动态规划 摆花 题解
    NOIP2004普及组第3题 FBI树
    实况世界杯4小游戏链接
    poj2761(treap入门)
    最大连续子序列和(分治法)
    任意区间的最长连续递增子序列,最大连续子序列和
    lca转RMQ
  • 原文地址:https://www.cnblogs.com/tmortred/p/13160505.html
Copyright © 2011-2022 走看看