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

  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.

题解:

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:

 class Solution {
int [][] dirs = new int[][]{{1,0},{0,-1},{0,1},{-1,0}};
String [] turns = new String[]{"d", "l", "r", "u"};
public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
if(maze == null || maze.length == 0 || maze[0].length == 0){
return "impossible";
} if(Arrays.equals(ball, hole)){
return "";
} int m = maze.length;
int n = maze[0].length;
boolean [][] visited = new boolean[m][n];
PriorityQueue<Point> minHeap = new PriorityQueue<>((a,b) -> a.dis == b.dis ? a.moving.compareTo(b.moving) : a.dis-b.dis);
minHeap.add(new Point(ball[0], ball[1], 0, ""));
while(!minHeap.isEmpty()){
Point cur = minHeap.poll();
if(cur.x == hole[0] && cur.y == hole[1]){
return cur.moving;
} if(visited[cur.x][cur.y]){
continue;
} visited[cur.x][cur.y] = true;
for(int i = 0; i<4; i++){
int r = cur.x;
int c = cur.y;
int dis = cur.dis; 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){
r += dirs[i][0];
c += dirs[i][1];
dis++;
if(r == hole[0] && c == hole[1]){
break;
}
} minHeap.add(new Point(r, c, dis, cur.moving+turns[i]));
}
} return "impossible";
}
} class Point{
int x;
int y;
int dis;
String moving;
public Point(int x, int y, int dis, String moving){
this.x = x;
this.y = y;
this.dis = dis;
this.moving = moving;
}
}

类似The MazeThe Maze II.

LeetCode 499. The Maze III的更多相关文章

  1. [LeetCode] 499. The Maze III 迷宫 III

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  2. LC 499. The Maze III 【lock,hard】

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  3. [LeetCode] 505. The Maze II 迷宫 II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  4. [LeetCode] 505. The Maze II 迷宫之二

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  5. [LeetCode] 490. The Maze 迷宫

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  6. LeetCode 490. The Maze

    原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...

  7. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  8. LeetCode:组合总数III【216】

    LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...

  9. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. Scala Class etc. 2

    Higher-Order Functions def 定义的是方法,而不是函数 函数可作为变量存在,可直接调用,也可作为值传递给其他函数 _ 后缀将普通方法变为函数: ceil _ 根据上下文编译器可 ...

  2. 【题解】Luogu P5301 [GXOI/GZOI2019]宝牌一大堆

    原题传送门 首先先要学会麻将,然后会发现就是一个暴力dp,分三种情况考虑: 1.非七对子国士无双,设\(dp_{i,j,k,a,b}\)表示看到了第\(i\)种牌,一共有\(j\)个\(i-1\)开头 ...

  3. 【在 Nervos CKB 上做开发】Nervos CKB 脚本编程简介[4]:在 CKB 上实现 WebAssembly

    作者:Xuejie 原文链接:https://xuejie.space/2019_10_09_introduction_to_ckb_script_programming_wasm_on_ckb/ N ...

  4. SQL Server的外键必须引用的是主键或者唯一键(转载)

    问: In SQL Server , I got this error -> "There are no primary or candidate keys in the refere ...

  5. javascript中五种迭代方法实例

    温习一下js中的迭代方法. <script type="text/javascript"> var arr = [1, 2, 3, 4, 5, 4, 3, 2, 1]; ...

  6. unable to retrieve container logs for docker kubernetes

    参考 https://github.com/knative/docs/issues/300 This is what happens when the build is successful and ...

  7. 很带劲,Android9.0可以在i.MX8开发板上这样跑

    米尔MYD-JX8MX开发板移植了Android9.0操作系统,现阶段最高版本的Android9.0操作系统将给您的产品在安全与稳定性方面带来更大的提升.可惜了,这里不能上传视频在i.MX8开发板跑A ...

  8. Django 连接 MySQL 数据库及常见报错解决

    目录 Django 连接 MySQL数据库及常见报错解决 终端或者数据库管理工具连接 MySQL ,并新建项目所需数据库 安装访问 MySQL 的 Python 模块 Django 相关配置 可能会遇 ...

  9. Vue.js---指令与事件、语法糖

    指令与事件 指令(Directives)是Vue.js模板中最常用的一项功能,它带有前缀v-,指令的职责就是当其表达式的值改变时,相应地将某些行为应用到DOM上. v-if: 显示这段文本 当数据sh ...

  10. Python学习的开端

    C语言太麻烦了,所以我打算自学Python. 自学选的书是<父与子的编程之旅>,这本书还是比较通俗易懂的. 贴上书上教我编写的猜数字游戏代码 import random secret = ...