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 ...
随机推荐
- 生成Makefile文件全过程
[1]生成Makefile文件全过程 整体流程如下图: 注意:以下文件根目录为testmake(任意位置新建即可) (1)测试程序 1.1 建立两个目录:mkdir include source 1. ...
- C++之救济金发放问题
n(n<20)个人站成一圈,逆时针编号为1~n.有两个官员,A从1开始逆时针数,B从n开始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个官员停在同一个人上).接 ...
- Akka-CQRS(12)- akka-http for http-web-service: Routing-服务项目接口
上篇提到,按当前对web-service功能需要,我们需要完成数据转换marshalling,服务接口routing这两部分的调研和示范.上篇已经完成了对序列化marshalling的讨论,这篇就介绍 ...
- Mysql系列(三)—— Mysql主从复制配置
一.前言 主从复制是Mysql知识体系中非常重的要一个模块.学习主从复制和后续的读写分离是完善只是知识体系的重要环节.且主从复制读写分离的思想并不仅仅局限于Mysql,在很多存储系统中都有该方案,如: ...
- ServerSocketChannel简述
一.前言 前篇文章中了解了SocketChannel:提供了连接到套接字通道,从某种层面而言,NIO中提供了类似于java.net包中对于网络操作的api的功能.既然已经有连接到Socket套接字的通 ...
- 让div在body中任意拖动
HTML代码 <div id="idOuterDiv" class="CsOuterDiv"> </div> CSS代码 body { ...
- datax分析与思考(一)
Datax 总体流程图 先看执行的第一个步骤: 在最上层抽象类,这个里面相当于获取全局公共信息,java入口部分就是这个Engine的main方法直接启动 Engine 启动 com.alibaba. ...
- Python基础7
深复制 & 浅复制 列表,字符串 都有深浅复制,用 id() 函数来看 所谓“旧瓶装新酒,新瓶装旧酒”
- Fedora搭dokuwiki的步骤 以apache2.4为例
官网下载dokuwiki的包,解压到/var/www/html/下 修改dokuwiki的权限.拥有者/组 为apache 安装PHP 在/etc/httpd/conf 创建dokuwiki的配置文件 ...
- nodeType属性在vue源码中的使用
每个节点都有一个 nodeType 属性,用于表明节点的类型,节点类型由 Node 类型中定义12个常量表示: nodeType在vue中的应用 在vue编译的过程中需要查找html结构中的双大括号 ...