【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 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

解题思路:很显然是动态规划,因为只能往左,上,左上三个方向移动,记dp_val[i][j]为从右下角移动到(i,j)时可以获得的最大值,那么有dp[i][j] = max(dp[i+1][j],dp[i][j+1],dp[i+1][j+1] , 因为还需要求出获得最大值时一共有几种移动路径,记dp_count[i][j]为从右下角移动到(i,j)获得最大值时移动路径的数量,只要相应的三个方向移动到(i,j)时可以得到最大值,那么dp_count[i][j] += dp_counti+x][j+y] 。 此外,还有不能从起点移动到终点的情况,所以在计算之前,可以用DFS/BFS先做一次判断。

代码如下:

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)]
        

 

上一篇:原题链接在这里:980. Unique Paths III


下一篇:zookeeper(10)源码分析-事件监听Watcher(3)