【leetcode】1301. Number of Paths with Max Score

Topics are as follows:

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

Outline of Solution: It is clear that dynamic programming, because only the left, upper, upper-left movement in three directions, denoted dp_val [i] [j] may be obtained from the maximum value is moved to the lower right corner (i, j), the then there dp [i] [j] = max (dp [i + 1] [j], dp [i] [j + 1], dp [i + 1] [j + 1], because the need to obtain the required when the maximum value of a total of several moving path, denoted dp_count [i] [j] is moved to the maximum value is obtained from the lower right (i, j) the number of the path of movement, as long as the direction corresponding to the three (i, j can be obtained when the maximum value), then the dp_count [i] [j] + = dp_counti + x] [j + y]. in addition, there can not be moved from the start point to the end of the case, so before calculations can DFS / BFS do first judgment.

code show as below:

class Solution(object):
    def canReach(self,board):
        queue = [(len(board)-1,len(board)-1)]
        dic = {}
        dic[(len(board)-1,len(board)-1)] = 1
        while len(queue) > 0:
            i,j = queue.pop(0)
            if i == 0 and j == 0:
                return True
            direction = [(-1, 0), (0, -1), (-1, -1)]
            for (x, y) in direction:
                if (i + x) >= 0 and (i + x) < len(board) and (j + y) >= 0 and (j + y) < len(board) \
                        and board[i+x][j+y] != 'X' and (i+x,j+y) not in dic:
                    queue.append((i+x,j+y))
                    dic[(i+x,j+y)] = 1
        return False


    def pathsWithMaxScore(self, board):
        """
        :type board: List[str]
        :rtype: List[int]
        """
        if self.canReach(board) == False:
            return [0,0]
        dp_val = [[0] * len(board) for _ in board]
        dp_count = [[0] * len(board) for _ in board]
        dp_count[-1][-1] = 1

        for i in range(len(board)-1,-1,-1):
            for j in range(len(board)-1,-1,-1):
                if board[i][j] == 'X':continue
                direction = [(1,0),(0,1),(1,1)]
                for (x,y) in direction:
                    if (i + x) >= 0 and (i + x) < len(board) and (j + y) >= 0 and (j + y) < len(board):
                        item_val = 0
                        if board[i+x][j+y] == 'X':
                            continue
                        if board[i][j] != 'S' and board[i][j] != 'E':
                            item_val = int(board[i][j])
                        if dp_val[i][j] < dp_val[i+x][j+y] + item_val:
                            dp_val[i][j] = dp_val[i + x][j + y] + item_val

                for (x,y) in direction:
                    if (i + x) >= 0 and (i + x) < len(board) and (j + y) >= 0 and (j + y) < len(board):
                        item_val = 0
                        if board[i][j] != 'S' and board[i][j] != 'E':
                            item_val = int(board[i][j])
                        if dp_val[i][j] == dp_val[i + x][j + y] + item_val:
                            dp_count[i][j] += dp_count[i + x][j + y]
        return [dp_val[0][0],dp_count[0][0] % (10**9 + 7)]
        

 

Guess you like

Origin www.cnblogs.com/seyjs/p/12114161.html