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 ...
随机推荐
- ndk-build官方使用说明
ndk-build 脚本可用于编译采用 NDK 基于 Make 的编译系统的项目.此外,我们还针对 ndk-build 使用的 Android.mk和 Application.mk 配置提供了更具体的 ...
- vue-cli中跨域问题解决方法
webpack提供了配置代理的方法解决跨域 1 在vue-cli项目中打开webpack.dev.cof.js,如下 2 打开conifg目录下的index.js,在 proxyTable中进行配置 ...
- 在ASP.NET Core中获取客户端和服务器端的IP地址(转载)
随着ASP.NET的发展,有不同的方式从请求中访问客户端IP地址.WebForms和MVC Web应用程序只是访问当前HTTP上下文的请求. var ip = HttpContext.Current. ...
- APS.NET MVC + EF (02)---深入理解ADO.NET Entity Framework
2.7 深入理解Entity Framework 性能问题几乎是一切ORM框架的通病,对于EF来说,引起性能低的原因主要在以下几个方面. 复杂的对象管理机制为了在.NET中更好地管理模型对象,EF提供 ...
- Vert.x(vertx)发送 HTTP/HTTPS请求
Vert.x Web服务有两种协议,一种是HTTP,另外一种是使用ssl的HTTPS,请求的方式有五种,分别是get.post.put.delete.head.为了简单,服务端主要实现对HTTP协议的 ...
- jquery.validate.unobtrusive的使用
应用 一.引入 <script src="Scripts/jquery-1.7.1.min.js"></script> <script src=&qu ...
- ASP.NET SignalR 系列(七)之服务端触发推送
前面几章讲的都是从客户端触发信息推送的,但在实际项目中,很多信息可能是由系统服务端推送的,下面2图分别展示两种通道 客户端触发推送 服务端推送 下面我们就重点介绍下服务端如何调用集线器的对象进行推送 ...
- 对比度增强(二):直方图正规划与伽马变换 cv.normal()函数使用及原理
直方图正规化: 图像为I,宽为W,高为H,I(r,c)代表I的第r行第c列的灰度值:输出图像记为O,为使得输出图像的灰度值在[Omin,Omax]范围里,可用如下公式: ...
- 504 Gateway Time-out ( Nginx + PHP ) 解决小计
问题 最近有个项目,运算量比较大,服务器经常报 504 Gateway Time-out 解决方案 fastcgi_connect_timeout 默认值是 60 第一次尝试修改为 120 ,仍然报5 ...
- node.js中的fs.rename()方法
node.js 中的fs.rename()模块 var fs=require('fs');//node.js的核心模块 原生模块.修改文件名称,可更改文件的存放路径 方法说明 : 修改文件名称,可更改 ...