原题链接在这里: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. 2019 C/C++《阿里》面试题总结

    一.C和C++的区别是什么? C是面向过程的语言,C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛. C中函数不能进行重载,C++函数可以重载 C++在C的基础上增添类,C是一个结构化语言 ...

  2. css z-index 的学习

    前言:这是笔者第一次写博客,主要是学习之后自己的理解.如果有错误或者疑问的地方,请大家指正,我会持续更新! z-index属性描述元素的堆叠顺序(层级),意思是 A 元素可以覆盖 B 元素,但是 B ...

  3. maven中pom的继承以及dependencies与dependencyManagement的区别

    https://blog.csdn.net/zzm3280/article/details/84952623 分类专栏: maven   本文转自:https://blog.csdn.net/liut ...

  4. lnmp1.4安装包

    https://lnmp.org/install.html nginx中虚拟机中的配置 location ~ .*\.(php|php5)?$ { try_files $uri =404; fastc ...

  5. php for循环a到z

    首先先介绍2个php内置函数 ord(string):函数返回字符串的首个字符的 ASCII 值.//string:必需.要从中获得 ASCII 值的字符串. chr(ascll): 函数从指定的 A ...

  6. 4、VUE生命周期

    下面是分步骤解释vue生命周期 1.开始:new Vue() 创建vue对象过程还是比较繁琐的,所以创建vue对象是异步执行的. 回调函数:beforeCreate 2.Observe Data 监控 ...

  7. App.config 分开文件

    App.config 分开文件 分开文件里配置: <appSettings configSource="App_TestMachine.config"> App_Tes ...

  8. Git 管理版本/回退

    参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/896954074659008 Git status命令可以让我们时刻掌握仓库当前的状态,比 ...

  9. Java IO---字节流和字符流

    一.IO流简介 流 流是一个抽象概念,Java程序和外部设备(可以是硬盘上的文件,也可以是网络设备)之间的输入输出操作是基于流的. 流就好比水管中的水流,具有流入和流出,类比数据的输入和输出. Jav ...

  10. nodejs vue的安装

    1.https://nodejs.org/en/ 下载最新版nodejs 2.安装好后win+R输入cmd(管理员权限键入):node -v(node版本)npm -v(npm版本)查看版本号,如图所 ...