Leetcode: The Maze(Unsolved locked 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.
Leetcode: The Maze(Unsolved locked problem)_第1张图片 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.
Leetcode: The Maze(Unsolved locked problem)_第2张图片 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.
  • Search in the four possible directions when coming to a stopping point (i.e. a new starting point).
  • Keep track of places that you already started at in case you roll back to that point.

 

 1 public class Solution {
 2     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
 3         boolean[][] startedHere = new boolean[maze.length][maze[0].length]; // mark visited starting points
 4         return dfs(maze, startedHere, start, destination);
 5     }
 6     
 7     private boolean dfs(int[][] maze, boolean[][] startedHere, int[] start, int[] destination) {
 8         if (startedHere[start[0]][start[1]]) return false;
 9         if (Arrays.equals(start, destination)) return true;
10         
11         startedHere[start[0]][start[1]] = true; // in case we roll back to a point we already started at
12         
13         BiPredicate roll = (rowInc, colInc) -> {
14             int row = start[0], col = start[1]; // init new start row and col
15             while (canRoll(maze, row + rowInc, col + colInc)) {
16                 row += rowInc;
17                 col += colInc;
18             }
19             return dfs(maze, startedHere, new int[]{row, col}, destination); // pass in new start to dfs
20         };
21         
22         if (roll.test(1, 0)) return true; // roll up
23         if (roll.test(0, 1)) return true; // roll right
24         if (roll.test(-1, 0)) return true; // roll down
25         if (roll.test(0, -1)) return true; // roll left
26         
27         return false; // return false if no paths led to destination
28     }
29     
30     private boolean canRoll(int[][] maze, int row, int col) {
31         if (row >= maze.length || row < 0 || col >= maze[0].length || col < 0) return false; // stop at borders
32         return maze[row][col] != 1; // stop at walls (1 -> wall)
33     }
34 }

UPDATE: Also including one without using BiPredicate on every recursive call since it runs faster

 1 public class Solution {
 2     
 3     private static final int[] DIRECTIONS = { 0, 1, 0, -1, 0 };
 4     
 5     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
 6         boolean[][] startedHere = new boolean[maze.length][maze[0].length];
 7         return dfs(maze, startedHere, start, destination);
 8     }
 9     
10     private boolean dfs(int[][] maze, boolean[][] startedHere, int[] start, int[] destination) {
11         if (startedHere[start[0]][start[1]]) return false;
12         if (Arrays.equals(start, destination)) return true;
13         
14         startedHere[start[0]][start[1]] = true;
15         
16         for (int i = 0; i < DIRECTIONS.length - 1; i++) {
17             int[] newStart = roll(maze, start[0], start[1], DIRECTIONS[i], DIRECTIONS[i + 1]);
18             if (dfs(maze, startedHere, newStart, destination)) return true;
19         }
20         
21         return false;
22     }
23     
24     private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) {
25         while (canRoll(maze, row + rowInc, col + colInc)) {
26             row += rowInc;
27             col += colInc;
28         }
29         
30         return new int[]{row, col};
31     }
32     
33     private boolean canRoll(int[][] maze, int row, int col) {
34         if (row >= maze.length || row < 0 || col >= maze[0].length || col < 0) return false;
35         return maze[row][col] != 1; // 1 is a wall
36     }
37 }

 

你可能感兴趣的:(Leetcode: The Maze(Unsolved locked problem))