[LeetCode] 505. The Maze II

There is a ball in a maze with empty spaces (represented as 0) and walls (represented as 1). The ball can go through the 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 m x n maze, the ball's start position and the destination, where start = [startrow, startcol] and destination = [destinationrow, destinationcol], return the shortest distance for the ball to stop at the destination. If the ball cannot stop at destination, return -1.

The distance is the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included).

You may assume that the borders of the maze are all walls (see examples).

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。

由空地和墙组成的迷宫中有一个球。球可以向上下左右四个方向滚动,但在遇到墙壁前不会停止滚动。当球停下时,可以选择下一个方向。

给定球的起始位置,目的地和迷宫,找出让球停在目的地的最短距离。距离的定义是球从起始位置(不包括)到目的地(包括)经过的空地个数。如果球无法停在目的地,返回 -1。

迷宫由一个0和1的二维数组表示。 1表示墙壁,0表示空地。你可以假定迷宫的边缘都是墙壁。起始位置和目的地的坐标通过行号和列号给出。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/the-maze-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 m = maze.length;
 4         int n = maze[0].length;
 5         int[][] distance = new int[m][n];
 6         // distance matrix
 7         for (int i = 0; i < m; i++) {
 8             for (int j = 0; j < n; j++) {
 9                 distance[i][j] = Integer.MAX_VALUE;
10             }
11         }
12         distance[start[0]][start[1]] = 0;
13         int[][] dirs = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
14         Queue<int[]> queue = new LinkedList<>();
15         queue.offer(start);
16         while (!queue.isEmpty()) {
17             int[] cur = queue.poll();
18             for (int[] dir : dirs) {
19                 int x = cur[0] + dir[0];
20                 int y = cur[1] + dir[1];
21                 int count = 0;
22                 while (x >= 0 && y >= 0 && x < m && y < n && maze[x][y] == 0) {
23                     x += dir[0];
24                     y += dir[1];
25                     count++;
26                 }
27                 if (distance[cur[0]][cur[1]] + count < distance[x - dir[0]][y - dir[1]]) {
28                     distance[x - dir[0]][y - dir[1]] = distance[cur[0]][cur[1]] + count;
29                     queue.offer(new int[] { x - dir[0], y - dir[1] });
30                 }
31             }
32         }
33         return distance[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1
34                 : distance[destination[0]][destination[1]];
35     }
36 }

 

flood fill题型总结

LeetCode 题目总结

posted @ 2020-07-28 05:32  CNoodle  阅读(182)  评论(0编辑  收藏  举报