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:
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 Maze, The Maze II.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· BotSharp 5.0 MCP:迈向更开放的AI Agent框架
· 分享 3 款基于 .NET 开源且免费的远程桌面工具
· 在线聊天系统中的多窗口数据同步技术解密
· 2025,回顾出走的 10 年
· 【保姆级教程】windows 安装 docker 全流程