原题链接在这里:https://leetcode.com/problems/the-maze-iii/
题目:
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r), but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also a hole in this maze. The ball will drop into the hole if it rolls on to the hole.
Given the ball position, the hole position and the maze, find out how the ball could drop into the hole by moving the shortest distance. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the hole (included). Output the moving directions by using 'u', 'd', 'l' and 'r'. Since there could be several different shortest ways, you should output the lexicographically smallest way. If the ball cannot reach the hole, output "impossible".
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 ball and the hole coordinates are represented by row and column indexes.
Example 1:
Input 1: a maze represented by a 2D array 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 Input 2: ball coordinate (rowBall, colBall) = (4, 3) Input 3: hole coordinate (rowHole, colHole) = (0, 1) Output: "lul" Explanation: There are two shortest ways for the ball to drop into the hole. The first way is left -> up -> left, represented by "lul". The second way is up -> left, represented by 'ul'. Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul".
Example 2:
Input 1: a maze represented by a 2D array 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 Input 2: ball coordinate (rowBall, colBall) = (4, 3) Input 3: hole coordinate (rowHole, colHole) = (3, 0) Output: "impossible" Explanation: The ball cannot reach the hole.
Note:
- There is only one ball and one hole in the maze.
- Both the ball and hole 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 the width and the height of the maze won't exceed 30.
题解:
From the ball index, find the shortest distance to the hole.
Use a PriorityQueue minHeap to perform weighted BFS based on distance and lexicographical order.
When findind the hole, break and it to the minHeap.
When polling out of minHeap, find current point, if it is hole. If it is hole, it must be shortest distance to the hole.
Time Complexity: O(mnlogmn).
Space: O(mn).
AC Java:
1 class Solution { 2 int [][] dirs = new int[][]{{1,0},{0,-1},{0,1},{-1,0}}; 3 String [] turns = new String[]{"d", "l", "r", "u"}; 4 public String findShortestWay(int[][] maze, int[] ball, int[] hole) { 5 if(maze == null || maze.length == 0 || maze[0].length == 0){ 6 return "impossible"; 7 } 8 9 if(Arrays.equals(ball, hole)){ 10 return ""; 11 } 12 13 int m = maze.length; 14 int n = maze[0].length; 15 boolean [][] visited = new boolean[m][n]; 16 PriorityQueue<Point> minHeap = new PriorityQueue<>((a,b) -> a.dis == b.dis ? a.moving.compareTo(b.moving) : a.dis-b.dis); 17 minHeap.add(new Point(ball[0], ball[1], 0, "")); 18 while(!minHeap.isEmpty()){ 19 Point cur = minHeap.poll(); 20 if(cur.x == hole[0] && cur.y == hole[1]){ 21 return cur.moving; 22 } 23 24 if(visited[cur.x][cur.y]){ 25 continue; 26 } 27 28 visited[cur.x][cur.y] = true; 29 for(int i = 0; i<4; i++){ 30 int r = cur.x; 31 int c = cur.y; 32 int dis = cur.dis; 33 34 while(r+dirs[i][0]>=0 && r+dirs[i][0]<m && c+dirs[i][1]>=0 && c+dirs[i][1]<n && maze[r+dirs[i][0]][c+dirs[i][1]]==0){ 35 r += dirs[i][0]; 36 c += dirs[i][1]; 37 dis++; 38 if(r == hole[0] && c == hole[1]){ 39 break; 40 } 41 } 42 43 minHeap.add(new Point(r, c, dis, cur.moving+turns[i])); 44 } 45 } 46 47 return "impossible"; 48 } 49 } 50 51 class Point{ 52 int x; 53 int y; 54 int dis; 55 String moving; 56 public Point(int x, int y, int dis, String moving){ 57 this.x = x; 58 this.y = y; 59 this.dis = dis; 60 this.moving = moving; 61 } 62 }
类似The Maze, The Maze II.