LeetCode 505. The Maze II

原题链接在这里: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:

  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.

题解:

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.

Note: check polled out node has been visited. If not, mark it as visited.

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 }

Could use LinkedList and a dist array to record the shortest distance from start to this point.

For polled node, go to dist and get the step up to that node, then 4 dirs to the end, accumlated step.

If the new node, dist is -1, or dist > accumlated steps, then update it.

Eventually, return dist[d[0]][d[1]]. 

Time Complexity: O(mn).

Space:(mn).

Java:

 1 class Solution {
 2     public int shortestDistance(int[][] maze, int[] start, int[] destination) {
 3         if(maze == null || maze.length == 0 || maze[0].length == 0){
 4             return -1;
 5         }
 6         
 7         int m = maze.length;
 8         int n = maze[0].length;
 9         int [][] dist = new int[m][n];
10         for(int [] arr : dist){
11             Arrays.fill(arr, -1);
12         }
13         
14         LinkedList<int []> que = new LinkedList<>();
15         que.add(start);
16         dist[start[0]][start[1]] = 0;
17         int [][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
18         
19         while(!que.isEmpty()){
20             int [] cur = que.poll();
21             for(int [] dir : dirs){
22                 int x = cur[0];
23                 int y = cur[1];
24                 int step = dist[x][y];
25                 while(x + dir[0] >= 0 && x + dir[0] < m && y + dir[1] >= 0 && y + dir[1] < n && maze[x + dir[0]][y + dir[1]] == 0){
26                     x += dir[0];
27                     y += dir[1];
28                     step++;
29                 }
30                 
31                 if(dist[x][y] == -1 || dist[x][y] > step){
32                     dist[x][y] = step;
33                     que.add(new int[]{x, y});
34                 }
35             }
36         }
37         
38         return dist[destination[0]][destination[1]];
39     }
40 }

类似The Maze.

posted @ 2019-11-25 11:36  Dylan_Java_NYC  阅读(827)  评论(0编辑  收藏  举报