505. The Maze II

Question

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, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.

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: 12

Thinking:

  • Method: BFS
public class TheMazeII {
	public static class Node{
		int row, col, l;
		public Node(int row, int col, int l){
			this.row = row;
			this.col = col;
			this. l = l;
		}
	}
	 public static int shortestDistance(int[][] maze, int[] start, int[] destination){
		 if(maze == null || maze[0].length == 0) return 0;
		 int height = maze.length, width = maze[0].length;
		 int[][] path = new int[height][width];
		 for(int i = 0; i < height; i++)
			 for(int j = 0; j < width; j++)
				 path[i][j] = Integer.MAX_VALUE;
		 LinkedList<Node> queue = new LinkedList<>();
		 queue.add(new Node(start[0], start[1], 0));
		 while(!queue.isEmpty()){
			 Node cur = queue.poll();
			 if(cur.l > path[cur.row][cur.col]) continue;
			 path[cur.row][cur.col] = cur.l;
			 for(int i = 0; i < 4; i++){
				 int row = cur.row, col = cur.col, l = cur.l;
				 while(row >= 0 && row < height && col >= 0 && col < width && maze[row][col] == 0){
					 row += dir[i][0];
					 col += dir[i][1];
					 l ++;
				 }
				 row -= dir[i][0];
				 col -= dir[i][1];
				 l--;
				 if(l >= path[destination[0]][destination[1]])
				 	continue;
				 if(l < path[row][col]){
					 queue.add(new Node(row,col, l));
				 }
			 }
		 }
		 return path[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1: path[destination[0]][destination[1]];
	 }
}
  • Method 2: DFS
     	public static int shortestDistanceDFS(int[][] maze, int[] start, int[] destination){
           if(maze == null || maze.length == 0) return -1;
           int height = maze.length, width = maze[0].length;
           int[][] path = new int[height][width];
           for(int i = 0; i < height; i++)
               for(int j = 0; j < width; j++)
                   path[i][j] = Integer.MAX_VALUE;
           shortestDistanceDFS(maze, new Node(start[0], start[1], 0), destination, path);
           return path[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1: path[destination[0]][destination[1]];
       }
       private static void shortestDistanceDFS(int[][] maze, Node cur, int[] des, int[][] path){
           if(cur.l > path[des[0]][des[1]]) return;
           if(cur.l >= path[cur.row][cur.col]) return;
           path[cur.row][cur.col] = cur.l;
           if(cur.row == des[0] && cur.col == des[1]){
               path[des[0]][des[1]] = Math.min(cur.l, path[des[0]][des[1]]);
               return;
           }
           for(int i = 0; i < 4; i++){
               int row = cur.row, col = cur.col, l = cur.l;
               while(row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0){
                   row += dir[i][0];
                   col += dir[i][1];
                   l++;
               }
               row -= dir[i][0];
               col -= dir[i][1];
               l --;
               shortestDistanceDFS(maze, new Node(row, col, l), des, path);
           }
       }