Java recursively realizes the maze escape game

Java recursively realizes the maze escape game

1. Rules

  • digital meaning

    0 - way; 1 - wall; 2 - passage; 3 - dead end

  • Pathfinding strategy

    Down --> Right --> Up --> Left

2. Code implementation

public class MyClass {
    
    
    public static void main(String[] args){
    
    
        //迷宫地图,初始为 8 行 7 列
        //0 - 路; 1 - 墙; 2 - 通路; 3 - 死路
        int[][] map = {
    
    {
    
    1, 1, 1, 1, 1, 1, 1},
                       {
    
    1, 0, 0, 0, 0, 0, 1},
                       {
    
    1, 0, 0, 0, 0, 0, 1},
                       {
    
    1, 1, 1, 0, 0, 0, 1},
                       {
    
    1, 0, 0, 0, 0, 0, 1},
                       {
    
    1, 0, 0, 0, 1, 0, 1},
                       {
    
    1, 0, 0, 0, 1, 0, 1},
                       {
    
    1, 1, 1, 1, 1, 1, 1}};
        //起始坐标
        int x = 4, y = 3;

        //是否走得通
        System.out.println("迷宫是否走得通:" + findWay(map, x, y));

        //打印迷宫路线
        System.out.println("迷宫路线如下:");
        for (int i = 0; i < 8; i++) {
    
    
            for (int j = 0; j < 7; j++) {
    
    
                System.out.printf(map[i][j] + " ");
            }
            System.out.println();
        }
    }

    //递归
    public static boolean findWay(int[][] map, int x, int y) {
    
    
        //终点,设置为 map[6][5]
        if (map[6][5] == 2) {
    
    
            return true;
        } else {
    
    
            //边界,无法继续前进
            if (x < 1 || x > 6 || y < 1 || y > 5 || map[x][y] != 0) {
    
    
                return false;
            }

            //假定本格走得通
            map[x][y] = 2;

            //判断是否走得通
            //向下 --> 向右 --> 向上 --> 向左
            if (findWay(map, x + 1, y)) {
    
    
                return true;
            } else if (findWay(map, x, y + 1)) {
    
    
                return true;
            } else if (findWay(map, x - 1, y)) {
    
    
                return true;
            } else if (findWay(map, x, y - 1)) {
    
    
                return true;
            } else {
    
    
                //走不通则设为死路
                map[x][y] = 3;
                return false;
            }
        }
    }

}
  • Time complexity: In the worst case, each grid needs to be visited once. On each grid, the recursive function tries to search in four directions. Therefore, the total time complexity is O(4^N*M). This is because in the worst case, the depth of the recursive function is N*M, and each level of recursion has 4branches (four directions).
  • Space Complexity: The space complexity of a recursive function depends on the depth of the recursive calls. In the worst case, the recursion depth is N*M, so the space complexity is O(N*M). This is because each recursive call allocates some space on the stack to store parameters, local variables, and return addresses.

3. Running results

迷宫是否走得通:true
迷宫路线如下:
1 1 1 1 1 1 1 
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 0 0 0 1
1 3 3 2 2 2 1
1 3 3 3 1 2 1
1 3 3 3 1 2 1
1 1 1 1 1 1 1

Guess you like

Origin blog.csdn.net/qq_45256357/article/details/131963549