原题链接在这里:https://leetcode.com/problems/the-maze-ii/
题目:
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 Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right. The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
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: -1 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.
题解:
Use PriorityQueue to make sure smaller weight is polling out first.
Mark the current position visited when polling out the node, that means from start to this node, the shortest path has been found.
If polling out node has been marked as visited, then it means other shorter path has visited this node before.
Time Complexity: O(mn*logmn). m = maze.length. n = maze[0].length.
Space: O(mn).
AC Java:
1 class Solution { 2 int [][] dirs = new int[][]{{-1,0}, {1,0}, {0,-1}, {0,1}}; 3 4 public int shortestDistance(int[][] maze, int[] start, int[] destination) { 5 if(maze == null || maze.length == 0 || maze[0].length == 0){ 6 return -1; 7 } 8 9 int m = maze.length; 10 int n = maze[0].length; 11 boolean [][] visited = new boolean[m][n]; 12 PriorityQueue<int []> minHeap = new PriorityQueue<>((a,b) -> a[2]-b[2]); 13 minHeap.add(new int[]{start[0], start[1], 0}); 14 while(!minHeap.isEmpty()){ 15 int [] cur = minHeap.poll(); 16 17 // If smaller value has been found for cur before, skip 18 if(visited[cur[0]][cur[1]]){ 19 continue; 20 } 21 22 visited[cur[0]][cur[1]] = true; 23 if(cur[0] == destination[0] && cur[1] == destination[1]){ 24 return cur[2]; 25 } 26 27 for(int [] dir : dirs){ 28 int r = cur[0]; 29 int c = cur[1]; 30 int step = 0; 31 while(r+dir[0]>=0 && r+dir[0]<m && c+dir[1]>=0 && c+dir[1]<n && maze[r+dir[0]][c+dir[1]]==0){ 32 r += dir[0]; 33 c += dir[1]; 34 step++; 35 } 36 37 minHeap.add(new int[]{r, c, cur[2]+step}); 38 } 39 } 40 41 return -1; 42 } 43 }
类似The Maze.