The Maze
滚弹珠三题,都比较有意思,第一题题意主要是滚珠能到达的地方都是能停留的位置,也就是滚珠的四边至少有一边是这个迷宫的边界这样滚珠才能停下来,基本就是个最普通的BFS看亮点之间是否能到达,唯一需要注意的是这里不是往四边一格的范围内扩散了,而是需要一直滚到停下来为止
class Solution(object):
def hasPath(self, maze, start, destination):
"""
:type maze: List[List[int]]
:type start: List[int]
:type destination: List[int]
:rtype: bool
"""
if not maze and not maze[0]:
return False
n, m = len(maze), len(maze[0])
Point = collections.namedtuple('Point', ['x', 'y'])
queue = collections.deque([Point(start[0], start[1])])
checker = set((start[0], start[1],))
while queue:
node = queue.pop()
if node.x == destination[0] and node.y == destination[1]:
return True
for i, j in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
curRow, curCol = node.x, node.y
while 0 <= curRow + i < n and 0 <= curCol + j < m and maze[curRow + i][curCol + j] != 1:
curRow += i
curCol += j
if (curRow, curCol,) not in checker:
checker.add((curRow, curCol,))
queue.appendleft(Point(curRow, curCol))
return False