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

  1. Input 1: a maze represented by a 2D array
  2.  
  3. 0 0 0 0 0
  4. 1 1 0 0 1
  5. 0 0 0 0 0
  6. 0 1 0 0 1
  7. 0 1 0 0 0
  8.  
  9. Input 2: ball coordinate (rowBall, colBall) = (4, 3)
  10. Input 3: hole coordinate (rowHole, colHole) = (0, 1)
  11.  
  12. Output: "lul"
  13.  
  14. Explanation: There are two shortest ways for the ball to drop into the hole.
  15. The first way is left -> up -> left, represented by "lul".
  16. The second way is up -> left, represented by 'ul'.
  17. Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul".

Example 2:

  1. Input 1: a maze represented by a 2D array
  2.  
  3. 0 0 0 0 0
  4. 1 1 0 0 1
  5. 0 0 0 0 0
  6. 0 1 0 0 1
  7. 0 1 0 0 0
  8.  
  9. Input 2: ball coordinate (rowBall, colBall) = (4, 3)
  10. Input 3: hole coordinate (rowHole, colHole) = (3, 0)
  11.  
  12. Output: "impossible"
  13.  
  14. 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:

  1. class Solution {
  2. int [][] dirs = new int[][]{{1,0},{0,-1},{0,1},{-1,0}};
  3. String [] turns = new String[]{"d", "l", "r", "u"};
  4. public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
  5. if(maze == null || maze.length == 0 || maze[0].length == 0){
  6. return "impossible";
  7. }
  8.  
  9. if(Arrays.equals(ball, hole)){
  10. return "";
  11. }
  12.  
  13. int m = maze.length;
  14. int n = maze[0].length;
  15. boolean [][] visited = new boolean[m][n];
  16. PriorityQueue<Point> minHeap = new PriorityQueue<>((a,b) -> a.dis == b.dis ? a.moving.compareTo(b.moving) : a.dis-b.dis);
  17. minHeap.add(new Point(ball[0], ball[1], 0, ""));
  18. while(!minHeap.isEmpty()){
  19. Point cur = minHeap.poll();
  20. if(cur.x == hole[0] && cur.y == hole[1]){
  21. return cur.moving;
  22. }
  23.  
  24. if(visited[cur.x][cur.y]){
  25. continue;
  26. }
  27.  
  28. visited[cur.x][cur.y] = true;
  29. for(int i = 0; i<4; i++){
  30. int r = cur.x;
  31. int c = cur.y;
  32. int dis = cur.dis;
  33.  
  34. 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){
  35. r += dirs[i][0];
  36. c += dirs[i][1];
  37. dis++;
  38. if(r == hole[0] && c == hole[1]){
  39. break;
  40. }
  41. }
  42.  
  43. minHeap.add(new Point(r, c, dis, cur.moving+turns[i]));
  44. }
  45. }
  46.  
  47. return "impossible";
  48. }
  49. }
  50.  
  51. class Point{
  52. int x;
  53. int y;
  54. int dis;
  55. String moving;
  56. public Point(int x, int y, int dis, String moving){
  57. this.x = x;
  58. this.y = y;
  59. this.dis = dis;
  60. this.moving = moving;
  61. }
  62. }

类似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. 宝塔webhook配合码云,本地git push 服务器自动pull

    emmmm,这其实是一个很简单的一件事情,但是有很多坑,记录一下 先大概讲一下原理吧,就是每次您 push 代码后,都会给远程 HTTP URL 发送一个 POST 请求 更多说明 » 然后在宝塔这边 ...

  2. [终极巨坑]golang+vue开发日记【一】,环境搭建篇

    写在前面 这个golang+vue大部分的内容是基于bydmm(橙卡)大佬的视频学来的,我在这里只是做一下个人开发的笔记,就是图一个乐,毕竟我只是个应届毕业生,如果真的要学请:bydmm的b站空间. ...

  3. 微信小程序之使用函数防抖与函数节流

    函数防抖和函数节流都是老生常谈的问题了.这两种方式都能优化 js 的性能.有些人可能会搞混两个的概念.所以,我以自己的理解,来解释这两个概念的含义.并且列举在小程序中这两个方法的使用. 函数防抖: 英 ...

  4. Luogu5611 Ynoi2013 D2T2/牛客挑战赛32F 最大子段和 分块、分治

    传送门 之前一直咕着的,因为一些特殊的原因把这道题更掉算了-- 有一个对值域莫队+线段树的做法,复杂度\(O(n\sqrt{n} \log n)\)然而牛客机子实在太慢了没有希望(Luogu上精细实现 ...

  5. Git系列 —— 记一次Mac上git push时总是403的错误

    问题: 今天从github上clone下一个项目,然后修改后git push时总是出现: remote:Permission to lixyou/rw-split-plugin.git defined ...

  6. Asp.Net Core Web Api 使用 Swagger 生成 api 说明文档

    最近使用 Asp.Net Core Web Api 开发项目服务端.Swagger 是最受欢迎的 REST APIs 文档生成工具之一,进入我的视野.以下为学习应用情况的整理. 一.Swagger 介 ...

  7. VS开发C++控制台应用程序(示例)

    注:笔者使用的VS版本为2019.1.打开VS2019,选择文件 -> 新建 -> 项目 2.选择项目新建项目时选择C++“控制台应用”语言:C++平台:Windows项目类型:控制台 3 ...

  8. 嵌入式Web服务器boa在ARM平台的移植步骤

    1.下载http://www.boa.org/ 2.解压tar xzf boa-0.94.13.tar.gz 3.编译cd boa-0.94.13/src./configure 生成了makefile ...

  9. vue打包后页面显示空白但是不报错

    在使用vue打包的时候页面显示空白,并且控制台不报错 设置vue-router的时候mode设置为history模式了,这种模式要有后台的配合,一般还是使用哈希模式就不会出现页面空白了.

  10. 【 Android 】ViewPager + TabLayout + Fragment 数据初始化问题

    在 ViewPager 和 Fragment 配合使用的时候,ViewPager 会使用预加载机制,使得我们在没有切换到到对应页面时,就已经加载好了,这是个非常不好的用户体验. 所以本示例项目就诞生了 ...