1. 程式人生 > 實用技巧 >[LeetCode] 505. The Maze II

[LeetCode] 505. The Maze II

There is aballin a maze with empty spaces and walls. The ball can go through empty spaces by rollingup,down,leftorright, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

Given the ball'sstart position, thedestinationand themaze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number ofempty spacestraveled 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.

迷宮II。題目設定跟490題幾乎一樣,不過這個題問的是從起點到終點,需要的最短距離是多少。

思路依然是flood fill類的BFS的做法,但是需要用到一個額外的矩陣記錄最短距離。在做BFS的過程中,跟490題類似,還是把能停下的座標加入queue繼續迴圈,但是在while迴圈裡,座標每往前走一步,step就要++,以用來記錄距離。當球停下的時候,你就有機會結算一下,當前用的步數是否是最短的。

時間O(mn)

空間O(mn)

Java實現

 1 class Solution {
 2     public int shortestDistance(int[][] maze, int[] start, int[] destination) {
 3         int[][] distance = new int[maze.length][maze[0].length];
 4         for (int[] row : distance) {
 5             Arrays.fill(row, Integer.MAX_VALUE);
 6         }
 7         distance[start[0]][start[1]] = 0;
 8         int[][] dirs = {{0, 1}, {0, -1}, { -1, 0}, {1, 0}};
 9         Queue<int[]> queue = new LinkedList<>();
10         queue.add(start);
11         while (!queue.isEmpty()) {
12             int[] current = queue.poll();
13             for (int[] dir : dirs) {
14                 int x = current[0] + dir[0];
15                 int y = current[1] + dir[1];
16                 int count = 0;
17                 while (x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] == 0) {
18                     x += dir[0];
19                     y += dir[1];
20                     count++;
21                 }
22                 if (distance[current[0]][current[1]] + count < distance[x - dir[0]][y - dir[1]]) {
23                     distance[x - dir[0]][y - dir[1]] = distance[current[0]][current[1]] + count;
24                     queue.add(new int[] {x - dir[0], y - dir[1]});
25                 }
26             }
27         }
28         return distance[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : distance[destination[0]][destination[1]];
29     }
30 }

flood fill題型總結

LeetCode 題目總結