迷宫求解算法(Java)

迷宫求解算法一直是算法学习的经典,实现自然也是多种多样,包括动态规划,递归等实现,这里我们使用穷举求解,加深对栈的理解和应用

定义Position类用于存储坐标点

起点坐标为(1,1),终点坐标为(8,8)
地图打印在最下面

class Position {
    private int px;
    private int py;
    public Position(int px, int py) {
        this.px = px;
        this.py = py;
    }
    public int getPx() {
        return px;
    }
    public void setPx(int px) {
        this.px = px;
    }
    public int getPy() {
        return py;
    }
    public void setPy(int py) {
        this.py = py;
    }
}
这里我们简单介绍下move()函数

move函数分别向四个方向移动,然后将可行的path入栈.
注意,这里栈元素中每个栈元素Position都是new出来的,栈中存的是reference,
注意看下面这种写法:

currentPosition.setPy(currentPosition.getPy()+1);
stacks.push(currentPosition);

这种写法一度让我陷入困惑,因为pop出来的Position都是一样的,原因大家可能应该明白了。。。

 public void move() {
        if (moveRight()) {
            Position temp = new Position(currentPosition.getPx() + 1, currentPosition.getPy());
            test.add(temp);
            stacks.push(temp);
        } else if (moveBottom()) {
            Position temp = new Position(currentPosition.getPx(), currentPosition.getPy() + 1);
            test.add(temp);
            stacks.push(temp);
        } else if (moveTop()) {
            Position temp = new Position(currentPosition.getPx(), currentPosition.getPy() - 1);
            test.add(temp);
            stacks.push(temp);
        } else if (moveLeft()) {
            Position temp = new Position(currentPosition.getPx() - 1, currentPosition.getPy());
            test.add(temp);
            stack
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成迷宫可以使用深度优先搜索算法或者随机化Prim算法。以下是使用深度优先搜索算法生成迷宫Java代码示例: ```java import java.util.Random; public class MazeGenerator { private int[][] maze; private int width; private int height; public MazeGenerator(int width, int height) { this.width = width; this.height = height; maze = new int[height][width]; } public int[][] generateMaze() { // Initialize maze with all walls for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { maze[i][j] = 1; } } Random rand = new Random(); int startX = rand.nextInt(width); int startY = rand.nextInt(height); // Create path maze[startY][startX] = 0; createPath(startX, startY); return maze; } private void createPath(int x, int y) { int[][] directions = {{0,-1},{0,1},{-1,0},{1,0}}; Random rand = new Random(); // Shuffle directions for (int i = 0; i < directions.length; i++) { int randIndex = rand.nextInt(directions.length); int[] temp = directions[i]; directions[i] = directions[randIndex]; directions[randIndex] = temp; } for (int i = 0; i < directions.length; i++) { int newX = x + directions[i][0] * 2; int newY = y + directions[i][1] * 2; if (newX >= 0 && newX < width && newY >= 0 && newY < height && maze[newY][newX] == 1) { maze[y + directions[i][1]][x + directions[i][0]] = 0; maze[newY][newX] = 0; createPath(newX, newY); } } } } ``` 该代码使用递归实现深度优先搜索算法来构建迷宫。使用`generateMaze()`方法生成迷宫,返回一个二维数组表示迷宫,其中0表示通路,1表示墙壁。 对于求解算法,可以使用广度优先搜索算法或A*算法。以下是使用广度优先搜索算法解决迷宫问题的Java代码示例: ```java import java.util.LinkedList; import java.util.Queue; public class MazeSolver { private int[][] maze; private int[][] visited; private int startX; private int startY; private int endX; private int endY; private int width; private int height; public MazeSolver(int[][] maze, int startX, int startY, int endX, int endY) { this.maze = maze; this.startX = startX; this.startY = startY; this.endX = endX; this.endY = endY; this.width = maze[0].length; this.height = maze.length; visited = new int[height][width]; } public int[][] solve() { Queue<int[]> queue = new LinkedList<>(); queue.add(new int[]{startX, startY}); visited[startY][startX] = 1; while (!queue.isEmpty()) { int[] current = queue.poll(); if (current[0] == endX && current[1] == endY) { // Reached end break; } int[][] directions = {{0,-1},{0,1},{-1,0},{1,0}}; for (int i = 0; i < directions.length; i++) { int newX = current[0] + directions[i][0]; int newY = current[1] + directions[i][1]; if (newX >= 0 && newX < width && newY >= 0 && newY < height && maze[newY][newX] == 0 && visited[newY][newX] == 0) { visited[newY][newX] = visited[current[1]][current[0]] + 1; queue.add(new int[]{newX, newY}); } } } return visited; } } ``` 该代码使用广度优先搜索算法寻找从起点到终点的最短路径,返回一个二维数组表示每个节点到起点的距离。其中0表示未访问,-1表示墙壁,其他数字表示距离。最终路径可以通过反向遍历visited数组得到。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值