Leetcode: The Maze III(Unsolved Lock Problem)

本文介绍了一种在迷宫中寻找球从起始位置到目标洞口最短路径的算法。该算法通过遍历所有可能的方向,并记录下最短路径及路径字符串。如果存在多个最短路径,则返回字典序最小的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Each time, first add the direction to the path, and then go with that direction, checking for hole along the way. When hit a wall, try to turn, and go with the new direction. For the starting point, don't "go", jump directly to "turn" part.

 1 public class Solution {
 2     int min; //min distance to hole
 3     String minS; //min distance's path string
 4     int[] hole;
 5     int[][] maze; 
 6     int[][] map; //shortest distant traveling from ball to this point
 7     int[][] dirs = {{0,1},{-1,0},{1,0},{0,-1}}; //r, u, d, l
 8     public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
 9         this.min = Integer.MAX_VALUE; 
10         this.minS = null;
11         this.hole = hole; 
12         this.maze = maze; 
13         this.map = new int[maze.length][maze[0].length];
14         for(int i = 0; i<map.length; i++) Arrays.fill(map[i], Integer.MAX_VALUE); 
15         
16         move(ball[0], ball[1], 0, "", -1);
17         return (minS==null) ? "impossible" : minS;
18     }
19     
20     private void move(int r, int c, int cnt, String path, int dir){//dir is a index of dirs 
21         if(cnt > min || cnt > map[r][c]) return; //not a shortest route for sure 
22         if(dir!=-1){//if not from start point 
23             //add path 
24             if(dir==0) path+='r';
25             else if(dir==1) path+='u';
26             else if(dir==2) path+='d';
27             else path+='l';
28     
29             //roll along dir 
30             while(r>=0 && r<maze.length && c>=0 && c<maze[0].length && maze[r][c]==0){
31                 map[r][c] = Math.min(map[r][c], cnt); 
32                 if(r==hole[0] && c==hole[1]){//check hole
33                     if(cnt==min && path.compareTo(minS)<0){
34                         minS=path;
35                     }else if(cnt<min){
36                         min = cnt; 
37                         minS = path; 
38                     }
39                     return; 
40                 }
41                 r += dirs[dir][0];
42                 c += dirs[dir][1];
43                 cnt++;
44             }
45             r -= dirs[dir][0];//[r,c] is wall, need to walk back 1 step
46             c -= dirs[dir][1];
47             cnt--;
48         }
49         
50         //hit wall (or start) -> try to turn
51         for(int i = 0; i<dirs.length; i++){
52             if(dir == i) continue;//dont keep going
53             if(dir == (3-i)) continue;//dont go back
54             int newR = r + dirs[i][0];
55             int newC = c + dirs[i][1];
56             if(newR>=0 && newR<maze.length && newC>=0 && newC<maze[0].length && maze[newR][newC]==0){//can go
57                 move(r, c, cnt, path, i);
58             }
59         }
60     }
61 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6396047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值