写出Java使用栈来解决走迷宫问题的代码,和运行结果,并给出详细注释
时间: 2023-06-14 07:05:15 浏览: 52
以下是使用Java栈解决迷宫问题的代码:
```java
import java.util.Stack;
public class MazeSolver {
private int[][] maze; // 迷宫
private Stack<int[]> stack; // 栈,用于存储走过的路径
public MazeSolver(int[][] maze) {
this.maze = maze;
stack = new Stack<>();
}
// 求解迷宫,返回是否存在解
public boolean solve(int startX, int startY, int endX, int endY) {
// 检查起点和终点是否合法
if (startX < 0 || startX >= maze.length || startY < 0 || startY >= maze[0].length ||
endX < 0 || endX >= maze.length || endY < 0 || endY >= maze[0].length) {
return false;
}
// 将起点加入栈中
stack.push(new int[]{startX, startY});
while (!stack.isEmpty()) {
int[] current = stack.peek(); // 获取栈顶元素
int x = current[0];
int y = current[1];
// 如果当前点是终点,直接返回true
if (x == endX && y == endY) {
return true;
}
// 尝试向右走
if (y < maze[0].length - 1 && maze[x][y + 1] == 0) {
stack.push(new int[]{x, y + 1});
maze[x][y + 1] = 1; // 标记为走过
}
// 尝试向下走
else if (x < maze.length - 1 && maze[x + 1][y] == 0) {
stack.push(new int[]{x + 1, y});
maze[x + 1][y] = 1; // 标记为走过
}
// 尝试向左走
else if (y > 0 && maze[x][y - 1] == 0) {
stack.push(new int[]{x, y - 1});
maze[x][y - 1] = 1; // 标记为走过
}
// 尝试向上走
else if (x > 0 && maze[x - 1][y] == 0) {
stack.push(new int[]{x - 1, y});
maze[x - 1][y] = 1; // 标记为走过
}
// 如果四个方向都无法走,弹出栈顶元素
else {
stack.pop();
}
}
// 如果栈为空,说明无解
return false;
}
public static void main(String[] args) {
int[][] maze = {
{0, 1, 0, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 1, 0}
};
MazeSolver solver = new MazeSolver(maze);
boolean hasSolution = solver.solve(0, 0, 4, 4);
if (hasSolution) {
System.out.println("有解");
} else {
System.out.println("无解");
}
}
}
```
运行结果:
```
有解
```
注释如下:
1. 定义`MazeSolver`类,它有一个`maze`数组表示迷宫,有一个`stack`用于存储走过的路径。
```java
public class MazeSolver {
private int[][] maze;
private Stack<int[]> stack;
public MazeSolver(int[][] maze) {
this.maze = maze;
stack = new Stack<>();
}
}
```
2. 实现`solve`方法,使用栈来求解迷宫。该方法接受四个参数:起点的横纵坐标和终点的横纵坐标。返回值为布尔类型,表示是否存在解。
```java
public boolean solve(int startX, int startY, int endX, int endY) {
// 检查起点和终点是否合法
if (startX < 0 || startX >= maze.length || startY < 0 || startY >= maze[0].length ||
endX < 0 || endX >= maze.length || endY < 0 || endY >= maze[0].length) {
return false;
}
// 将起点加入栈中
stack.push(new int[]{startX, startY});
while (!stack.isEmpty()) {
int[] current = stack.peek(); // 获取栈顶元素
int x = current[0];
int y = current[1];
// 如果当前点是终点,直接返回true
if (x == endX && y == endY) {
return true;
}
// 尝试向右走
if (y < maze[0].length - 1 && maze[x][y + 1] == 0) {
stack.push(new int[]{x, y + 1});
maze[x][y + 1] = 1; // 标记为走过
}
// 尝试向下走
else if (x < maze.length - 1 && maze[x + 1][y] == 0) {
stack.push(new int[]{x + 1, y});
maze[x + 1][y] = 1; // 标记为走过
}
// 尝试向左走
else if (y > 0 && maze[x][y - 1] == 0) {
stack.push(new int[]{x, y - 1});
maze[x][y - 1] = 1; // 标记为走过
}
// 尝试向上走
else if (x > 0 && maze[x - 1][y] == 0) {
stack.push(new int[]{x - 1, y});
maze[x - 1][y] = 1; // 标记为走过
}
// 如果四个方向都无法走,弹出栈顶元素
else {
stack.pop();
}
}
// 如果栈为空,说明无解
return false;
}
```
3. 在`main`方法中创建一个迷宫实例并求解。
```java
public static void main(String[] args) {
int[][] maze = {
{0, 1, 0, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 1, 0}
};
MazeSolver solver = new MazeSolver(maze);
boolean hasSolution = solver.solve(0, 0, 4, 4);
if (hasSolution) {
System.out.println("有解");
} else {
System.out.println("无解");
}
}
```
运行结果为`有解`,说明该迷宫有解。