The Maze

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

DFS

对于dfs,如果当前“决定”对后续有影响,可以使用第16行这种方法不断递归。

 1 class Solution {
 2     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
 3         int m = maze.length, n = maze[0].length;
 4         boolean[][] visited = new boolean[m][n];
 5         return dfs(maze, visited, start, destination);
 6     }
 7     private boolean dfs(int[][] maze, boolean[][] visited, int[] start, int[] destination) {
 8         int row = start[0], col = start[1];
 9         if (row < 0 || row >= maze.length || col < 0 || col >= maze[0].length || visited[row][col]) return false;
10         visited[row][col] = true;
11         if (row == destination[0] && col == destination[1]) return true;
12         
13        int[] directions = { 0, 1, 0, -1, 0 };
14         for (int i = 0; i < directions.length - 1; i++) {
15             int[] newStart = roll(maze, start[0], start[1], directions[i], directions[i + 1]);
16             if (dfs(maze, visited, newStart, destination)) return true;
17         }
18         return false;
19     }
20     
21     private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) {
22         while (canRoll(maze, row + rowInc, col + colInc)) {
23             row += rowInc;
24             col += colInc;
25         }
26         return new int[]{row, col};
27     }
28     
29     private boolean canRoll(int[][] maze, int row, int col) {
30         if (row >= maze.length || row < 0 || col >= maze[0].length || col < 0 || maze[row][col] == 1) return false;
31         return true;
32     }
33 }

BFS

 1 class Solution {
 2     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
 3         Deque<int[]> queue = new ArrayDeque<>();
 4         boolean[][] visited = new boolean[maze.length][maze[0].length];
 5         queue.offer(start);
 6         while (!queue.isEmpty()) {
 7             int[] cur = queue.poll();
 8             int row = cur[0], col = cur[1];
 9             if (row == destination[0] && col == destination[1]) {
10                 return true;
11             }
12             if (visited[row][col]) {
13                 continue;
14             }
15             visited[row][col] = true;
16 
17             int[] directions = { 0, 1, 0, -1, 0 };
18             for (int i = 0; i < directions.length - 1; i++) {
19                 int[] newStart = roll(maze, row, col, directions[i], directions[i + 1]);
20                 queue.offer(newStart);
21             }
22         }
23         return false;
24     }
25 
26     private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) {
27         while (canRoll(maze, row + rowInc, col + colInc)) {
28             row += rowInc;
29             col += colInc;
30         }
31         return new int[] { row, col };
32     }
33 
34     private boolean canRoll(int[][] maze, int row, int col) {
35         if (row >= maze.length || row < 0 || col >= maze[0].length || col < 0 || maze[row][col] == 1)
36             return false;
37         return true;
38     }
39 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/11146097.html

你可能感兴趣的:(The Maze)