Leetcode: The Maze(Unsolved locked problem)

本文介绍了一种在迷宫中寻找从起点到终点路径的方法。通过递归深度优先搜索(DFS),并结合滚动机制,算法能够判断球是否能从起点出发到达指定目的地。此方法适用于由空地和墙壁组成的迷宫。

摘要生成于 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, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.

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 start and destination coordinates are represented by row and column indexes.

Example 1

Input 1: a maze represented by a 2D array

0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0

Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4)

Output: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
Example
2 Input 1: a maze represented by a 2D array 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0 Input 2: start coordinate (rowStart, colStart) = (0, 4) Input 3: destination coordinate (rowDest, colDest) = (3, 2) Output: false Explanation: There is no way for the ball to stop at the destination.
Note: There is only one ball and one destination in the maze. Both the ball and the destination 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 both the width and height of the maze won't exceed 100.
  • Search in the four possible directions when coming to a stopping point (i.e. a new starting point).
  • Keep track of places that you already started at in case you roll back to that point.

 

 1 public class Solution {
 2     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
 3         boolean[][] startedHere = new boolean[maze.length][maze[0].length]; // mark visited starting points
 4         return dfs(maze, startedHere, start, destination);
 5     }
 6     
 7     private boolean dfs(int[][] maze, boolean[][] startedHere, int[] start, int[] destination) {
 8         if (startedHere[start[0]][start[1]]) return false;
 9         if (Arrays.equals(start, destination)) return true;
10         
11         startedHere[start[0]][start[1]] = true; // in case we roll back to a point we already started at
12         
13         BiPredicate<Integer, Integer> roll = (rowInc, colInc) -> {
14             int row = start[0], col = start[1]; // init new start row and col
15             while (canRoll(maze, row + rowInc, col + colInc)) {
16                 row += rowInc;
17                 col += colInc;
18             }
19             return dfs(maze, startedHere, new int[]{row, col}, destination); // pass in new start to dfs
20         };
21         
22         if (roll.test(1, 0)) return true; // roll up
23         if (roll.test(0, 1)) return true; // roll right
24         if (roll.test(-1, 0)) return true; // roll down
25         if (roll.test(0, -1)) return true; // roll left
26         
27         return false; // return false if no paths led to destination
28     }
29     
30     private boolean canRoll(int[][] maze, int row, int col) {
31         if (row >= maze.length || row < 0 || col >= maze[0].length || col < 0) return false; // stop at borders
32         return maze[row][col] != 1; // stop at walls (1 -> wall)
33     }
34 }

UPDATE: Also including one without using BiPredicate on every recursive call since it runs faster

 1 public class Solution {
 2     
 3     private static final int[] DIRECTIONS = { 0, 1, 0, -1, 0 };
 4     
 5     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
 6         boolean[][] startedHere = new boolean[maze.length][maze[0].length];
 7         return dfs(maze, startedHere, start, destination);
 8     }
 9     
10     private boolean dfs(int[][] maze, boolean[][] startedHere, int[] start, int[] destination) {
11         if (startedHere[start[0]][start[1]]) return false;
12         if (Arrays.equals(start, destination)) return true;
13         
14         startedHere[start[0]][start[1]] = true;
15         
16         for (int i = 0; i < DIRECTIONS.length - 1; i++) {
17             int[] newStart = roll(maze, start[0], start[1], DIRECTIONS[i], DIRECTIONS[i + 1]);
18             if (dfs(maze, startedHere, newStart, destination)) return true;
19         }
20         
21         return false;
22     }
23     
24     private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) {
25         while (canRoll(maze, row + rowInc, col + colInc)) {
26             row += rowInc;
27             col += colInc;
28         }
29         
30         return new int[]{row, col};
31     }
32     
33     private boolean canRoll(int[][] maze, int row, int col) {
34         if (row >= maze.length || row < 0 || col >= maze[0].length || col < 0) return false;
35         return maze[row][col] != 1; // 1 is a wall
36     }
37 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值