# 490 The Maze

## 1. Question

There is a ballin 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 destinationand 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.
Example 1
Input 1: a maze represented by a 2D array
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
Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4)
Output: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right. Example 2
Input 1: a maze represented by a 2D array
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
Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (3, 2)
Output: false
Explanation: There is no way for the ball to stop at the destination. Note:
1. 1.
There is only one ball and one destination in the maze.
2. 2.
Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
3. 3.
The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
4. 4.
The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.

## 2. Implementation

(1) BFS
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
if (maze == null || maze.length == 0 || maze.length == 0) {
return false;
}
int m = maze.length, n = maze.length;
boolean[][] visited = new boolean[m][n];
visited[start][start] = true;
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!queue.isEmpty()) {
int[] curCell = queue.remove();
int curRow = curCell, curCol = curCell;
if (curRow == destination && curCol == destination) {
return true;
}
for (int[] direction : directions) {
int nextRow = curRow + direction;
int nextCol = curCol + direction;
while (isValid(maze, nextRow, nextCol, visited)) {
nextRow += direction;
nextCol += direction;
}
nextRow -= direction;
nextCol -= direction;
if (!visited[nextRow][nextCol]) {
visited[nextRow][nextCol] = true;
}
}
}
return false;
}
public boolean isValid(int[][] maze, int row, int col, boolean[][] visited) {
return row >= 0 && row < maze.length && col >= 0 && col < maze.length && maze[row][col] == 0;
}
}
(1) DFS
public class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
if (maze == null || maze.length == 0 || maze.length == 0) {
return false;
}
int m = maze.length, n = maze.length;
boolean[][] visited = new boolean[m][n];
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
return searchPathByDFS(maze, start, destination, visited, directions);
}
public boolean searchPathByDFS(int[][] maze, int[] start, int[] destination, boolean[][] visited, int[][] directions) {
int curRow = start;
int curCol = start;
if (visited[curRow][curCol]) {
return false;
}
visited[curRow][curCol] = true;
if (curRow == destination && curCol == destination) {
return true;
}
for (int[] direction : directions) {
int nextRow = curRow + direction;
int nextCol = curCol + direction;
while (isValid(nextRow, nextCol, maze)) {
nextRow += direction;
nextCol += direction;
}
nextRow -= direction;
nextCol -= direction;
if (searchPathByDFS(maze, new int[] {nextRow, nextCol}, destination, visited, directions)) {
return true;
}
}
return false;
}
public boolean isValid(int row, int col, int[][] maze) {
return row >= 0 && row < maze.length && col >= 0 && col < maze.length && maze[row][col] == 0;
}
}

## 3. Time & Space Complexity

BFS:时间复杂度O(mn), 空间复杂度Omn)
DFS:时间复杂度O(mn), 空间复杂度Omn)