[LeetCode] 499. The Maze III

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. There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.

Given the m x n maze, the ball's position ball and the hole's position hole, where ball = [ballrow, ballcol] and hole = [holerow, holecol], return a string instructions of all the instructions that the ball should follow to drop in the hole with the shortest distance possible. If there are multiple valid instructions, return the lexicographically minimum one. If the ball can't drop in the hole, return "impossible".

If there is a way for the ball to drop in the hole, the answer instructions should contain the characters 'u' (i.e., up), 'd' (i.e., down), 'l' (i.e., left), and 'r' (i.e., right).

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 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:

  1. There is only one ball and one hole in the maze.
  2. Both the ball and hole 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 the width and the height of the maze won't exceed 30.

迷宫III。

由空地和墙组成的迷宫中有一个球。球可以向上(u)下(d)左(l)右(r)四个方向滚动,但在遇到墙壁前不会停止滚动。当球停下时,可以选择下一个方向。迷宫中还有一个洞,当球运动经过洞时,就会掉进洞里。

给定球的起始位置,目的地和迷宫,找出让球以最短距离掉进洞里的路径。 距离的定义是球从起始位置(不包括)到目的地(包括)经过的空地个数。通过'u', 'd', 'l' 和 'r'输出球的移动方向。 由于可能有多条最短路径, 请输出字典序最小的路径。如果球无法进入洞,输出"impossible"。

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/the-maze-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意跟前两个版本类似,多一个条件,矩阵中有一个坐标hole代表一个洞,问的是球是否能按规则移动最后掉入洞口,若能则输出字典序最小的路径,若不能则输出impossible。

思路还是BFS,但是具体写的时候有一些细节需要注意。

如果遍历的过程中,有遇到hole的坐标则立即break,开始结算路径

路径的长度这样算:起点和终点的横纵坐标的绝对值的差

用compareTo来比较字典序

代码非常琐碎,面试不太容易写的对

时间O(mn)

空间O(n)

Java实现

 1 class Solution {
 2     public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
 3         int m = maze.length;
 4         int n = maze[0].length;
 5         String[][] way = new String[m][n];
 6         for (int i = 0; i < m; i++) {
 7             for (int j = 0; j < n; j++) {
 8                 way[i][j] = "";
 9             }
10         }
11 
12         int[][] dp = new int[m][n];
13         int[] dx = new int[] { -1, 1, 0, 0 };
14         int[] dy = new int[] { 0, 0, -1, 1 };
15         String[] dircectStr = new String[] { "u", "d", "l", "r" };
16         Queue<int[]> queue = new LinkedList<>();
17         queue.offer(ball);
18         while (!queue.isEmpty()) {
19             int[] cur = queue.poll();
20             for (int direction = 0; direction < 4; direction++) {
21                 int nx = cur[0];
22                 int ny = cur[1];
23                 while (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] == 0) {
24                     if (nx == hole[0] && ny == hole[1]) {
25                         nx += dx[direction];
26                         ny += dy[direction];
27                         break;
28                     }
29                     nx += dx[direction];
30                     ny += dy[direction];
31                 }
32                 nx -= dx[direction];
33                 ny -= dy[direction];
34                 int steps = dp[cur[0]][cur[1]] + Math.abs(nx - cur[0]) + Math.abs(ny - cur[1]);
35                 // 非当前位置,未初始化,路径长度小于当前位置的原来的值,路径长度相等,字典序更小
36                 if (!(nx == cur[0] && ny == cur[1]) && (dp[nx][ny] == 0 || (dp[nx][ny] > steps || (dp[nx][ny] == steps
37                         && (way[cur[0]][cur[1]] + dircectStr[direction]).compareTo(way[nx][ny]) < 0)))) {
38                     dp[nx][ny] = steps;
39                     way[nx][ny] = way[cur[0]][cur[1]] + dircectStr[direction];
40                     if (!(nx == hole[0] && ny == hole[1])) {
41                         queue.offer(new int[] { nx, ny });
42                     }
43                 }
44             }
45         }
46         return way[hole[0]][hole[1]].equals("") ? "impossible" : way[hole[0]][hole[1]];
47     }
48 }

 

flood fill题型总结

LeetCode 题目总结

posted @ 2020-07-28 14:41  CNoodle  阅读(245)  评论(0编辑  收藏  举报