leetcode576. path out of bounds number

Path out of bounds number

Title : Given a grid and a ball of m × n. The initial coordinates of the ball is (i, j), can you move to the ball adjacent cells, or upward, moving the ball through the mesh boundary on the lower, left, and right directions. However, you can move up to N times. Find out the number of paths can be moved out of the boundary of the ball. The answer may be very large, the return value is the result mod 10 ^ 9 + 7.

method one

A start watching question, that the robot path problem Similarly, direct dfs iteration N times, from the starting point, a position reachable stack, determines whether the current position is out of bounds, if out of bounds, the number of paths plus one; otherwise, it next stack position reachable.

But this question is similar to (did) the title is somewhat different in the past, the path to this question is with the return of , say, an access point can be repeated several times. This way, the very large amount of calculation is repeated. (In fact, the results returned from the title mod 10 ^ 9 + 7 to look out, not a small amount of calculation wow.)

Method Two

Due to a time-out method, we must think of an algorithm to solve this problem with a memory of a large number of double-counting features. So, dynamic programming it coming!
Since each movement of the first movement is a result of the decision on the ball , the number of states after each movement by accumulating, at the boundary position of the ball (when the ball is in the position of the boundary, the next one will be able to move out of bounds) and by in this boundary position from several directions may be out of bounds (CNT <=. 3) . So you can get the number of paths final ball out of bounds.

Wherein the dynamic transfer equation:
DP [I] [J] [K] = DP [I-. 1] [J] [K-. 1] + DP [I +. 1] [J] [K-. 1] + DP [ I] [J-. 1] [-K. 1] + DP [I] [J +. 1] [-K. 1] , K is the number of moves.

Initialization problem here, for simplicity, eliminating the need to discuss the border issue, initialize the matrix to be large given matrix circle. Thus, each node in the update value, when the current is determined not cross, whether ordinate bounds.

class Solution(object):
    def findPaths(self, m, n, N, i, j):
        """
        :type m: int
        :type n: int
        :type N: int
        :type i: int
        :type j: int
        :rtype: int
        """
        lis = [[[0 for p in range(n+2)] for q in range(m+2)]]
        lis[0][i+1][j+1] = 1 #初始化,第0次移动起始节点值为1
        ans = 0              #路径个数
        k = 0                #移动次数
        while k < N:
            temp = [[0 for x in range(n+2)] for y in range(m+2)]
            for p in range(1,m+1):
                for q in range(1,n+1):
                    if lis[k][p][q] > 0: 
                        cnt = 0   #计算可出界的方向数
                        if p == 1:
                            cnt += 1
                        if p == m:
                            cnt += 1
                        if q == 1:
                            cnt += 1
                        if q == n:
                            cnt += 1
                        ans += cnt * lis[k][p][q] 
                    temp[p][q] = lis[k][p-1][q] + lis[k][p+1][q] + lis[k][p][q-1] + lis[k][p][q+1]        
            lis.append(temp)
            k += 1
        return ans % (10 ** 9 + 7)
Released two original articles · won praise 2 · views 60

Guess you like

Origin blog.csdn.net/weixin_38022038/article/details/104797607