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 = [startrow, startcol]
and destination = [destinationrow, destinationcol]
, 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.length
n == maze[i].length
1 <= m, n <= 100
maze[i][j]
is0
or1
.start.length == 2
destination.length == 2
0 <= startrow, destinationrow <= m
0 <= startcol, destinationcol <= 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.
DFS.
class Solution:
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
def dfs(maze, start, destination):
if visited[start[0]][start[1]]:
return False
if start[0] == destination[0] and start[1] == destination[1]:
return True
visited[start[0]][start[1]] = True
l, r, u, d = start[1] - 1, start[1] + 1, start[0] - 1, start[0] + 1
while l >= 0 and maze[start[0]][l] == 0:
l -= 1
if dfs(maze, [start[0], l + 1], destination):
return True
while r < len(maze[0]) and maze[start[0]][r] == 0:
r += 1
if dfs(maze, [start[0], r - 1], destination):
return True
while u >= 0 and maze[u][start[1]] == 0:
u -= 1
if dfs(maze, [u + 1, start[1]], destination):
return True
while d < len(maze) and maze[d][start[1]] == 0:
d += 1
if dfs(maze, [d - 1, start[1]], destination):
return True
return False
visited = [[False for _ in maze[0]] for _ in maze]
return dfs(maze, start, destination)
class Solution {
private boolean[][] visited;
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int m = maze.length, n = maze[0].length;
visited = new boolean[m][n];
return dfs(maze, start, destination);
}
private boolean dfs(int[][] maze, int[] start, int[] destination) {
if (visited[start[0]][start[1]]) return false;
if (start[0] == destination[0] && start[1] == destination[1]) return true;
visited[start[0]][start[1]] = true;
int l = start[1] - 1, r = start[1] + 1, u = start[0] - 1, d = start[0] + 1;
while (l >= 0 && maze[start[0]][l] == 0) --l;
if (dfs(maze, new int[]{start[0], l + 1}, destination)) return true;
while (r < maze[0].length && maze[start[0]][r] == 0) ++r;
if (dfs(maze, new int[]{start[0], r - 1}, destination)) return true;
while (u >= 0 && maze[u][start[1]] == 0) --u;
if (dfs(maze, new int[]{u + 1, start[1]}, destination)) return true;
while (d < maze.length && maze[d][start[1]] == 0) ++d;
if (dfs(maze, new int[]{d - 1, start[1]}, destination)) return true;
return false;
}
}