The Maze Problem
Description
LeetCode Problem 490.
There is a ball in a maze with empty spaces (represented as 0) and walls (represented as 1). The ball can go through the 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 m x n maze, the ball's start position and the destination, where start = [start_row, start_col] and destination = [destination_row, destination_col], return true if the ball can stop at the destination, otherwise return false.
You may assume that the borders of the maze are all walls (see examples).
Example 1:

Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4]
Output: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
Example 2:

Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2]
Output: false
Explanation: There is no way for the ball to stop at the destination. Notice that you can pass through the destination but you cannot stop there.
Example 3:
Input: maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], start = [4,3], destination = [0,1]
Output: false
Constraints:
m == maze.lengthn == maze[i].length1 <= m, n <= 100maze[i][j]is0or1.start.length == 2destination.length == 20 <= start_row, destination_row < m0 <= start_col, destination_col < n- Both the ball and the destination exist in an empty space, and they will not be in the same position initially.
- The maze contains at least 2 empty spaces.
Solution
Approach: BFS over Stopping Cells
The ball cannot stop wherever it likes: once it starts rolling, it only stops right before a wall or the border. So the real "nodes" of this search are the cells where the ball can stop, and from each such cell there are at most four moves, one per direction.
Run a breadth-first search from start:
- Take a stopping cell from the queue. If it is the destination, return
true. - For each of the four directions, roll the ball: keep stepping while the next cell is inside the maze and empty. The cell where it ends is the next stopping cell.
- If that cell has not been seen yet, mark it and add it to the queue.
If the queue runs out, the destination can never be a stopping cell, so return false.
The tricky part is what to mark. Only the cells where the ball stops go into visited, not the cells it rolls over. Example 2 shows why: the ball passes through [3,2] but can never stop there, so the answer is false even though the cell is reached. Marking cells along the way would also be wrong, because the same cell can be rolled over in one direction and stopped on in another.
A depth-first search works just as well here; BFS avoids deep recursion on a 100 x 100 maze.
Complexity: O(mn · max(m, n)) time: each of the mn cells is a stopping cell at most once, and each of its four rolls takes up to max(m, n) steps. O(mn) space for visited and the queue.
from collections import deque
class Solution:
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
m, n = len(maze), len(maze[0])
visited = [[False] * n for _ in range(m)] # cells where the ball has stopped
visited[start[0]][start[1]] = True
queue = deque([(start[0], start[1])])
while queue:
i, j = queue.popleft()
if [i, j] == destination:
return True
for a, b in ((-1, 0), (1, 0), (0, -1), (0, 1)):
# roll until the next cell is a wall or outside the maze
x, y = i, j
while 0 <= x + a < m and 0 <= y + b < n and maze[x + a][y + b] == 0:
x, y = x + a, y + b
if not visited[x][y]:
visited[x][y] = True
queue.append((x, y))
return False