一、迷宫Ⅰ
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right.
Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
深度优先搜索-->
如何记录下一个要访问的节点:stack/recursion ;如何记录已经访问过的节点:set ;如何记录访问路径:dict
该题只返回true/false,不需要记录路径,不需要dict。set-->用一个一模一样大小的矩阵来记录。
1 def dfs(matrix, start, dest): 2 visited = [[False] * len(matrix[0]) for i in range(len(matrix))] 3 return dfsHelper(matrix, start, dest, visited) 4 def dfsHelper(matrix, start, dest, visited): 5 if matrix[start[0]][start[1]] == 1: 6 return False 7 if visited[start[0]][start[1]]: 8 return False 9 if start[0] == dest[0] and start[1] == dest[1]: 10 return True 11 visited[start[0]][start[1]] = True 12 if start[1] < len(matrix[0]) - 1: 13 r = (start[0], start[1] + 1) 14 if dfsHelper(matrix, r, dest, visited): 15 return True 16 if start[1] > 0: 17 l = (start[0], start[1]-1) 18 if dfsHelper(matrix, l, dest, visited): 19 return True 20 if start[0] > 0: 21 u = (start[0] - 1, start[1]) 22 if dfsHelper(matrix, u, dest, visited): 23 return True 24 if start[0] < len(matrix[0]) - 1: 25 d = (start[0] + 1, start[1]) 26 if dfsHelper(matrix, d, dest, visited): 27 return True 28 return False 29 30 matrix = [ 31 [0, 0, 1, 0, 0], 32 [0, 0, 0, 0, 0], 33 [0, 0, 0, 1, 0], 34 [1, 1, 0, 0, 1], 35 [0, 0, 0, 0, 0] 36 ] 37 38 start = (0, 0) 39 dest = (4, 4) 40 print(dfs(matrix, start, dest))
1 def dfs(matrix, start, dest): 2 visited = [[False]*(len(matrix[0])) for i in range(len(matrix))] 3 stack = [start] 4 visited[start[0]][start[1]] = True 5 idxs = [[0, 1], [0, -1], [-1, 0], [1, 0]] 6 while len(stack) != 0: 7 p = stack.pop() 8 if p[0] == dest[0] and p[1] == dest[1]: 9 return True 10 for idx in idxs: 11 x = p[0] + idx[0] 12 y = p[1] + idx[1] 13 if x < 0 or x >= len(matrix) or y < 0 or y >= len(matrix): 14 continue 15 if matrix[x][y] == 1: 16 continue 17 if visited[x][y] == 1: 18 continue 19 visited[x][y] = True 20 stack.append((x, y)) 21 return False
1 from collections import deque 2 def bfs(matrix, start, dest): 3 visited = [[False]*(len(matrix)) for i in range(len(matrix))] 4 q = deque() 5 q.append(start) 6 visited[start[0]][start[1]] = True 7 idxs = [[0, 1], [0, -1], [1, 0], [-1, 0]] 8 while len(q) != 0: 9 cur = q.popleft() 10 if cur[0] == dest[0] and cur[1] == dest[1]: 11 return True 12 for idx in idxs: 13 x = cur[0] + idx[0] 14 y = cur[1] + idx[1] 15 if x < 0 or x >= len(matrix) or y < 0 or y >= len(matrix): 16 continue 17 if matrix[x][y] == 1: 18 continue 19 if visited[x][y] == True: 20 continue 21 visited[x][y] = True 22 q.append((x, y)) 23 return False 24 25 matrix = [ 26 [0, 0, 1, 0, 0], 27 [0, 0, 0, 0, 0], 28 [0, 0, 0, 1, 0], 29 [1, 1, 0, 1, 1], 30 [0, 0, 0, 0, 0] 31 ] 32 33 start = (0, 0) 34 dest = (4, 4) 35 print(bfs(matrix, start, dest))
二 迷宫Ⅱ
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right. but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
1 def dfs(matrix, start, dest): 2 visited = [[False]*len(matrix) for i in range(len(matrix))] 3 return dfsHelper(matrix, start, dest, visited) 4 def dfsHelper(matrix, start, dest, visited): 5 if matrix[start[0]][start[1]] == 1: 6 return False 7 if visited[start[0]][start[1]]: 8 return False 9 if start[0] == dest[0] and start[1] == dest[1]: 10 return True 11 visited[start[0]][start[1]] = True 12 r = start[1] + 1 13 l = start[1] - 1 14 u = start[0] - 1 15 d = start[0] + 1 16 while r < len(matrix) and matrix[start[0]][r] == 0: 17 r += 1 18 x = (start[0], r-1) 19 if dfsHelper(matrix, x, dest, visited): 20 return True 21 while l >= 0 and matrix[start[0]][l] == 0: 22 l -= 1 23 x = (start[0], l+1) 24 if dfsHelper(matrix, x, dest, visited): 25 return True 26 while u >= 0 and matrix[u][start[1]] == 0: 27 u -= 1 28 x = (u+1, start[1]) 29 if dfsHelper(matrix, x, dest, visited): 30 return True 31 while d < len(matrix) and matrix[d][start[1]] == 0: 32 d += 1 33 x = (d-1, start[1]) 34 if dfsHelper(matrix, x, dest, visited): 35 return True 36 return False 37 38 matrix = [ 39 [0, 0, 1, 0, 0], 40 [0, 0, 0, 0, 0], 41 [0, 0, 0, 1, 0], 42 [1, 1, 0, 1, 1], 43 [0, 0, 0, 0, 0] 44 ] 45 46 start = (0, 0) 47 dest = (3, 2) 48 print(dfs(matrix, start, dest))
三 迷宫Ⅲ
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
Given the ball's start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.