Title: On-dimensional mesh grid, there are four types of boxes:
a square represents start. And only one starting grid.
2 shows the end of the square, and only one end of the square.
0 means that we can walk in the empty box.
-1 indicates obstacle we can not overcome.
Back in four directions (up, down, left, right) when walking on the starting grid from the number of different paths to the end of squares, each square must be accessible by one.
Source: https://leetcode-cn.com/problems/unique-paths-iii/
Act one: their own code
Ideas: The basic backtracking algorithm, note that for loop when a variable is assigned to temp, do not assign coor, back when the transfer function of the variable error-prone!
# Execute when used: 72 ms, beat the 52.94% of users in all python3 submission # memory consumption: 12.8 MB, defeated 100.00% of users in all python3 submission from Typing Import List class Solution: DEF uniquePathsIII (Self, Grid: List [List [int]]) -> int: r = len (grid) c = len (grid [0]) coordinate = [] bar = [] ANS = [0] # record can go grid coordinates of the start point, the end point for I in Range (R & lt): for J in Range (C): IF Grid [I] [J] in [0]: coordinate.append((i,j)) elif grid[i][j] == 1: start = (i,j) elif grid[i][j] == 2: end = (i,j) else: bar.append((i,j)) coordinate.append(end) Dire = [[0,1], [1,0], [0, -1], [-. 1 , 0]] DEF recursion (COOR): # if the current cell is the end, only two cases, one is to find a path, and the other is half way near the end, must be the path does not meet the conditions, the direct return IF coor == End: IF len (Coordinate) == 0: ANS [0] + =. 1 return for P, Q in Dire: # particular attention here to assign temp, is not coor, if coor then, for the second cycle for the first time is directly circulated in based on the modified value, TEMP = (COOR [0] + P, COOR [. 1] + Q) # If out of bounds, or go to the handicapped or grid has gone before, Continue IF (-1 <TEMP [0] <R & lt) + (-1 <TEMP [. 1] <C) = 2! or TEMP in bar or TEMP Not in Coordinate: Continue coordinate.remove(temp) recursion(coor=temp) coordinate.append(temp) recursion(start) return ans[0] if __name__ == '__main__': duixiang = Solution () a = duixiang.uniquePathsIII([[1,0,0,0],[0,0,0,0],[0,0,0,2]]) print(a)
ttt