LeetCode 499. The Maze III
原题链接在这里: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:
- There is only one ball and one hole in the maze.
- Both the ball and hole exist on an empty space, and they will not be at the same position initially.
- 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.
- 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 Maze, The Maze II.
LeetCode 499. The Maze III的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- LeetCode 490. The Maze
原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 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 ...
随机推荐
- CentOS7安装Node_exporter(二进制)
一.概述 Node_exporter是可以在* Nix和Linux系统上运行的计算机度量标准的导出器.Node_exporter 主要用于暴露 metrics 给 Prometheus,其中 metr ...
- 剑指offer之面试题2:实现Singleton模式
来源:剑指offer 这篇主要记录<剑指offer>书籍中的面试题2:实现Singleton模式 使用语言:C# 代码环境:VS2017 总共有5中解法,从前往后依次优化. 结构如下: 前 ...
- pacman 命令详解
Pacman 是一个命令行工具,这意味着当你执行下面的命令时,必须在终端或控制台中进行. 1.更新系统 在 Arch Linux 中,使用一条命令即可对整个系统进行更新:pacman -Syu 如果你 ...
- Laravel使用Redis共享Session
一.当系统的访问量上升的时候,使用Redis保存Session可以提高系统的性能,同时也方便多机负载的时候共享Session 打开config/database.php.在redis中增加sessio ...
- 换个语言学一下 Golang (10)——并行计算
如果说Go有什么让人一见钟情的特性,那大概就是并行计算了吧. 做个题目 如果我们列出10以下所有能够被3或者5整除的自然数,那么我们得到的是3,5,6和9.这四个数的和是23.那么请计算1000以下( ...
- cmd脚本
管道命令 | |命令的作用,就是让前一命令的输出当做后一命令的输入. > >会清除掉原有文件中的内容后把新的内容写入原文件: echo @echo off > a.bat. > ...
- python基础--py2与py3编码
python2 与 python3的编码和解码 注意:小心,容易弄混 目录: 1.python2d 的encode & decode 2.python3的encode & decode ...
- vue跳转链接(新页签)
const {href} = this.$router.resolve({path:"/hosScreen"}); window.open(href, '_blank');
- underscore_1: map()
map()是underscore.js中一个处理数组和对象的方法. params: 1. array || obj 2. callback 3. content 上下文指向 使用: var obj = ...
- Android studio module生成jar包,module中引用的第三方库没有被引用,导致java.lang.NoClassDefFoundError错误。
android studio 创建了一个Module生成jar包,这个module中有引用一些第三方的类库,比如 gson,volley等. 但是生成的jar包里,并没有将gson,volley等第三 ...