递归解迷宫问题

问题描述

迷宫可以看成一个由房间组成的二维矩阵,其中有一个入口(左上)、一个出口(右下),其他位置可以是堵塞的墙或可进的房间,要求从入口出发,寻找一个到达一个出口的路径。本解只涉及上、下、左、右四个运动方向。其中的1表示墙壁,0表示可以走的位置。

运行示例

在这里插入图片描述

源码

/**
 * @author Spring-_-Bear
 * @version 2021/10/7 8:15
 */
public class MazeGame {
    
    
    /**
     * The height of the game map.
     */
    static final int ROWS = 25;
    /**
     * The width of the game map.
     */
    static final int COLS = 25;
    /**
     * The two-dimensional array stands for the game map.
     */
    static int[][] map = new int[ROWS][COLS];

    /**
     * The state of current location.
     */
    enum State {
    
    
        /**
         * COULD_GO - 0: The current location was never passed by, could go with judge success.
         * OBSTACLE - 1: There is an obstacle in the current location, don't go.
         * ACCESSIBLE - 2: The current location is safe, could go.
         * DON_NOT_GO_AGAIN: You have judged the current location, don't go again.
         */
        COULD_GO, OBSTACLE, ACCESSIBLE, DON_NOT_GO_AGAIN;
    }

    /**
     * Initialize the boundary of game map, value is 1.
     * Randomly insert obstacles,value is 1.
     *
     * @param map Two-dimensional array.
     */
    public void initMap(int[][] map) {
    
    

        // Initialize the boundary of game map, values is 1.
        // Initialize the value of the first row of the two-dimensional game map to 1
        for (int i = 0; i < MazeGame.COLS; i++) {
    
    
            map[0][i] = 1;
        }
        // Initialize the element value of the last row of the two-dimensional game map to 1
        for (int i = 0; i < MazeGame.COLS; i++) {
    
    
            map[MazeGame.ROWS - 1][i] = 1;
        }
        // Initialize the value of the first column of the two-dimensional game map to 1
        for (int i = 0; i < MazeGame.ROWS; i++) {
    
    
            map[i][0] = 1;
        }
        // Initialize the value of the last column of the two-dimensional game map to 1
        for (int i = 0; i < MazeGame.ROWS; i++) {
    
    
            map[i][MazeGame.COLS - 1] = 1;
        }

        // Randomly insert obstacles,values is 1.
        for (int i = 0; i < MazeGame.ROWS; i++) {
    
    
            // num: Initialized number of obstacles in each row.
            int num = ((int) (Math.random() * MazeGame. COLS) + 1) / 2;
            for(int j = 0; j < num; j++){
    
    
                // Initial position
                int location = (int) (Math.random() * MazeGame.COLS);
                map[i][location] = State.OBSTACLE.ordinal();
            }
        }
    }


    /**
     * Print the two-dimensional game map.
     *
     * @param map Two-dimensional array.
     */
    public void printMap(int[][] map) {
    
    
        for (int i = 0; i < MazeGame.COLS; i++) {
    
    
            for (int j = 0; j < MazeGame.COLS; j++) {
    
    
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    /**
     * Try to find the way to go through the maze.
     *
     * @param map The two-dimensional game map.
     * @param row The abscissa of the initial position.
     * @param col The ordinate of the initial position.
     * @return
     */
    public boolean findWay(int[][] map, int row, int col) {
    
    
        // Recursive exit, reaching the end, game is over
        if (map[MazeGame.ROWS - 2][MazeGame.COLS - 2] == State.ACCESSIBLE.ordinal()) {
    
    
            return true;
        } else {
    
    
            // Did not find the recursive exit, continue to recursively find the exit
            // Current location was never passed by, you should judge whether it could go.
            if (map[row][col] == State.COULD_GO.ordinal()) {
    
    
                // First, we assumed to be able to go through, make the value is 2
                map[row][col] = State.ACCESSIBLE.ordinal();

                // To the right of the current position is a path
                if (findWay(map, row, col + 1)) {
    
    
                    return true;
                } else if (findWay(map, row + 1, col)) {
    
    
                    // Below the current position is a pathway
                    return true;
                } else if (findWay(map, row, col - 1)) {
    
    
                    // To the left of the current position is a path
                    return true;
                } else if (findWay(map, row - 1, col)) {
    
    
                    // Above the current position is a pathway
                    return true;
                } else {
    
    
                    // There is no accessible way of the current location, change its value to 3.
                    map[row][col] = State.DON_NOT_GO_AGAIN.ordinal();
                    return false;
                }
            } else {
    
    
                // map[row][col] == 1 || 2 || 3
                return false;
            }
        }
    }

    public static void main(String[] args) {
    
    
        MazeGame mazeGame = new MazeGame();
        mazeGame.initMap(MazeGame.map);
        System.out.println("The initial game map:");
        mazeGame.printMap(MazeGame.map);
        if(mazeGame.findWay(map, 1, 1)){
    
    
            System.out.println("You Win!!!");
        }else{
    
    
            System.out.println("You Lose!!!");
        }

        mazeGame.printMap(MazeGame.map);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_51008866/article/details/120641899