 # [LeetCode] 490. The Maze (BFS/DFS)

## Problem

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.

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:

There is only one ball and one destination in the maze.
Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
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.
The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.

## Solution - DFS

``````class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int m = maze.length, n = maze.length;
boolean[][] visited = new boolean[m][n];
return dfs(maze, visited, start, destination);
}
private boolean dfs(int[][] maze, boolean[][] visited, int[] start, int[] destination) {
int row = start, col = start;

//check boundaries and if the point visited before
if (row < 0 || row >= maze.length || col < 0 || col >= maze.length || visited[row][col]) return false;

//mark as a visited stop point
visited[row][col] = true;

//if stop point is destination => true
if (row == destination && col == destination) return true;

//DFS on four directions
int[][] dirs = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
for (int i = 0; i < 4; i++) {
int x = row, y = col;

//rolling until out or hit the wall
while (x >= 0 && x < maze.length && y >= 0 && y < maze.length && maze[x][y] != 1) {
x += dirs[i];
y += dirs[i];
}

//back to the stop point
x -= dirs[i];
y -= dirs[i];

//start another dfs from the stop point
if (dfs(maze, visited, new int[]{x, y}, destination)) return true;
}
return false;
}
}``````

## Solution - BFS

``````class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int m = maze.length, n = maze.length;
Deque<int[]> queue = new ArrayDeque<>();
boolean[][] visited = new boolean[m][n];
queue.offer(start);
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int row = cur, col = cur;
if (row == destination && col == destination) return true;
if (visited[row][col]) continue;
visited[row][col] = true;
int[][] dirs = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
for (int[] dir: dirs) {
int x = row;
int y = col;
while (x >= 0 && x < maze.length && y >= 0 && y < maze.length && maze[x][y] == 0) {
x += dir;
y += dir;
}
x -= dir;
y -= dir;
queue.offer(new int[]{x, y});
}
}
return false;
}
}``````