490 The Maze

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
1
Input 1: a maze represented by a 2D array
2
3
0 0 1 0 0
4
0 0 0 0 0
5
0 0 0 1 0
6
1 1 0 1 1
7
0 0 0 0 0
8
9
Input 2: start coordinate (rowStart, colStart) = (0, 4)
10
Input 3: destination coordinate (rowDest, colDest) = (4, 4)
11
12
Output: true
13
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
Copied!
Example 2
1
Input 1: a maze represented by a 2D array
2
3
0 0 1 0 0
4
0 0 0 0 0
5
0 0 0 1 0
6
1 1 0 1 1
7
0 0 0 0 0
8
9
10
Input 2: start coordinate (rowStart, colStart) = (0, 4)
11
12
Input 3: destination coordinate (rowDest, colDest) = (3, 2)
13
14
15
Output: false
16
17
Explanation: There is no way for the ball to stop at the destination.
Copied!
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
1
class Solution {
2
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
3
if (maze == null || maze.length == 0 || maze[0].length == 0) {
4
return false;
5
}
6
7
int m = maze.length, n = maze[0].length;
8
boolean[][] visited = new boolean[m][n];
9
10
Queue<int[]> queue = new LinkedList<>();
11
queue.add(start);
12
visited[start[0]][start[1]] = true;
13
14
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
15
16
while (!queue.isEmpty()) {
17
int[] curCell = queue.remove();
18
int curRow = curCell[0], curCol = curCell[1];
19
20
if (curRow == destination[0] && curCol == destination[1]) {
21
return true;
22
}
23
24
for (int[] direction : directions) {
25
int nextRow = curRow + direction[0];
26
int nextCol = curCol + direction[1];
27
28
while (isValid(maze, nextRow, nextCol, visited)) {
29
nextRow += direction[0];
30
nextCol += direction[1];
31
}
32
nextRow -= direction[0];
33
nextCol -= direction[1];
34
35
if (!visited[nextRow][nextCol]) {
36
visited[nextRow][nextCol] = true;
37
queue.add(new int[] {nextRow, nextCol});
38
}
39
}
40
}
41
return false;
42
}
43
44
public boolean isValid(int[][] maze, int row, int col, boolean[][] visited) {
45
return row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0;
46
}
47
}
Copied!
(1) DFS
1
public class Solution {
2
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
3
if (maze == null || maze.length == 0 || maze[0].length == 0) {
4
return false;
5
}
6
7
int m = maze.length, n = maze[0].length;
8
boolean[][] visited = new boolean[m][n];
9
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
10
11
return searchPathByDFS(maze, start, destination, visited, directions);
12
}
13
14
public boolean searchPathByDFS(int[][] maze, int[] start, int[] destination, boolean[][] visited, int[][] directions) {
15
int curRow = start[0];
16
int curCol = start[1];
17
18
if (visited[curRow][curCol]) {
19
return false;
20
}
21
22
visited[curRow][curCol] = true;
23
24
25
if (curRow == destination[0] && curCol == destination[1]) {
26
return true;
27
}
28
29
for (int[] direction : directions) {
30
int nextRow = curRow + direction[0];
31
int nextCol = curCol + direction[1];
32
33
while (isValid(nextRow, nextCol, maze)) {
34
nextRow += direction[0];
35
nextCol += direction[1];
36
}
37
nextRow -= direction[0];
38
nextCol -= direction[1];
39
40
if (searchPathByDFS(maze, new int[] {nextRow, nextCol}, destination, visited, directions)) {
41
return true;
42
}
43
}
44
return false;
45
}
46
47
public boolean isValid(int row, int col, int[][] maze) {
48
return row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0;
49
}
50
}
Copied!

3. Time & Space Complexity

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