解迷宫问题的Java实现

问题:

有一个m行n列的迷宫,只有一个入口和一个出口,用0表示可以走,用1表示不可以走,现在编写一个程序列出所有可以走的路径。

迷宫示例:

-1 0 0 0 0
1 1 0 1 1
0 0 0 0 0
0 0 2 0 0
(-1 表示入口,2表示出口)
MazeCell类表示迷宫的一个位置:
/**
 * @author cuiods
 */
public class MazeCell {
    private int x;
    private int y;
    private int step;

    public MazeCell(int x, int y, int step) {
        this.x = x;
        this.y = y;
        this.step = step;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }
}
Maze是迷宫类:
import java.util.Scanner;
import java.util.Stack;

/**
 * 迷宫类
 * 找到所有到达终点的路径
 * @author cuiods
 */
public class Maze {

    /**
     * 临时保存路径
     */
    private Stack<MazeCell> pathStack = new Stack<>();
    /**
     * 保存迷宫
     */
    private int[][] maze;
    private boolean flag = false;
    private MazeCell startCell;
    private MazeCell endCell;

    public Maze() {
        initialMaze();
    }

    /**
     * 寻找路径
     */
    public void findPath() {
        assert flag;
        processCell(startCell.getX(), startCell.getY(), startCell.getStep());
    }

    private void processCell(int x, int y, int step) {
        if (x == endCell.getX() && y == endCell.getY()) {
            pathStack.pop();
            printPath();
            System.out.println("("+endCell.getX()+","+endCell.getY()+")");
            return;
        }
        test(x,y-1,step+1);
        test(x,y+1,step+1);
        test(x-1,y,step+1);
        test(x+1,y,step+1);
    }

    private void test(int x, int y, int step) {
        if (canGo(x,y)){
            MazeCell mazeCell = new MazeCell(x,y,step);
            insertToPath(mazeCell);
            processCell(x,y,step);
        }
    }

    private void printPath(){
        for (int i = 0; i < pathStack.size(); i++) {
            MazeCell cell = pathStack.get(i);
            System.out.print("("+cell.getX()+","+cell.getY()+")->");
        }
    }

    private void insertToPath(MazeCell mazeCell) {
        while (pathStack.peek().getStep() >= mazeCell.getStep()) {
            pathStack.pop();
        }
        pathStack.push(mazeCell);
    }

    private boolean canGo(int x, int y) {
        if (maze[x][y]==1) {
            return false;
        }
        for (int i = 0; i < pathStack.size(); i++) {
            MazeCell mazeCell = pathStack.get(i);
            if (mazeCell.getX()==x && mazeCell.getY()==y) {
                return false;
            }
        }
        return true;
    }

    private void initialMaze() {
        int column;
        int row;
        Scanner scanner = new Scanner(System.in);
        int temp = 0;
        do {
            System.out.println("请输入迷宫行数(>0):");
            temp = scanner.nextInt();
        } while (temp<=0);
        row = temp;
        do {
            System.out.println("请输入迷宫列数(>0):");
            temp = scanner.nextInt();
        } while (temp<=0);
        column = temp;
        maze = new int[row+2][column+2];
        System.out.println("请输入迷宫(1为墙,0为路,-1为起点,2为终点):");
        for (int i = 0; i < column+2; i++) {
            maze[0][i] = 1;
        }
        for (int i = 1; i < row+1; i++) {
            maze[i][0] = 1;
            for (int j = 1; j < column+1; j++) {
                temp = scanner.nextInt();
                switch (temp) {
                    case -1:
                        startCell = new MazeCell(i,j,0);
                        maze[i][j] = temp;
                        pathStack.push(startCell);
                        break;
                    case 2:endCell = new MazeCell(i,j,-1);
                    case 0:
                    case 1:maze[i][j] = temp;break;
                    default:
                        System.out.println("输入不符合要求T T");
                        return;
                }
            }
            maze[i][column+1] = 1;
        }
        for (int i = 0; i < column+2; i++) {
            maze[row+1][i] = 1;
        }
        if (startCell!=null && endCell!=null) {
            flag = true;
            System.out.println("输入成功:)");
        } else {
            System.out.println("至少要有一个起点和终点:(");
        }
    }
}
测试的main类:
/**
 * @author cuiods
 * test main
 */
public class Main {

    public static void main(String[] args) {
        Maze maze = new Maze();
        maze.findPath();
    }
}

运行截图:





  • 13
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值