Java-mouse out of the maze (recursive)


Problem Description

insert image description here

A little mouse passes through some obstacles from the starting point to the end point, and the surrounding walls are impassable. Please design a maze path so that the mouse can escape the maze successfully.


1. Problem-solving ideas

1. Create a maze with a two-dimensional array first.
2. Use numbers to represent the different states of each element of the array
. 0 No obstacle
1 There is an obstacle
2 You can walk through it
3 You can walk through it but you can’t go
through it Set the row and the leftmost and rightmost two rows to 1 to indicate wall
4, and use 1 to indicate the obstacles in the array, and output the map

5. Create a method findway to find the path of the maze
6. Return true if found, otherwise return false
7. Determine the strategy for the mouse to find the way, which direction to go first

2. Write code

1. Create the Maze

insert image description here

code show as below:

public class MiGong {
    
    
    public static void main(String[] args) {
    
    

        //创建map数组表示一个八行七列的迷宫,第四行第2、3个为障碍物
        //0代表可以走,1代表障碍物
        int map[][] = new int[8][7];//八行七列的数组
        for (int i = 0; i < 7; i++) {
    
    
            map[0][i] = 1;
            map[7][i] = 1;//上下两行障碍物
        }
        for (int i = 0; i < 8; i++) {
    
    
            map[i][0] = 1;
            map[i][6] = 1;//左右两列障碍物
        }
        map[3][1] = 1;
        map[3][2] = 1;//第四行的两个障碍物
        //输出迷宫
        for (int i = 0; i < 8; i++) {
    
    
            for (int j = 0; j < 7; j++) {
    
    
                System.out.print(map[i][j]);//输出一行
            }
            System.out.println();//输出一行后换行
        }
    }
}

You will get an array as follows to represent the maze
insert image description here

2. Write the findway method

code show as below:

class F{
    
    

    //使用递归回溯思想写findway方法
    //i,j表示老鼠的位置,最初位置为(1,1)
    //当map[6][5]=2说明找到通路可以结束,否则继续找
    //确定找路策略  下 -> 右 -> 上 -> 左
    public boolean findway(int[][] map , int i , int j){
    
    //找到返回true,没找到返回false

        if(map[6][5] == 2){
    
    //已经找到
            return true;
        }else{
    
    
            if(map[i][j] == 0){
    
    //当前位置为0,可以走
                //假定可以走通
                map[i][j] = 2;
                //开始按策略找路
                if(findway(map, i + 1, j)){
    
    //先向下走
                    return true;
                }else if(findway(map, i, j + 1)){
    
    //向右
                    return true;
                }else if(findway(map, i - 1, j)){
    
    //向上
                    return true;
                }else if(findway(map, i, j - 1)){
    
    //向左
                    return true;
                }else{
    
    //假定可以走通是错误的
                    map[i][j] = 3;
                    return false;
                }
            }else{
    
    //map[i][j]=1,2,3
                return false;
            }
        }
    }
}

3. The overall code test is as follows

public class MiGong {
    
    
    public static void main(String[] args) {
    
    

        //创建map数组表示一个八行七列的迷宫,第四行第2、3个为障碍物
        //0代表可以走,1代表障碍物
        int map[][] = new int[8][7];//八行七列的数组
        for (int i = 0; i < 7; i++) {
    
    
            map[0][i] = 1;
            map[7][i] = 1;//上下两行障碍物
        }
        for (int i = 0; i < 8; i++) {
    
    
            map[i][0] = 1;
            map[i][6] = 1;//左右两列障碍物
        }
        map[3][1] = 1;
        map[3][2] = 1;//第四行的两个障碍物
        //输出迷宫
        for (int i = 0; i < 8; i++) {
    
    
            for (int j = 0; j < 7; j++) {
    
    
                System.out.print(map[i][j]);//输出一行
            }
            System.out.println();//输出一行后换行
        }

        //使用findway方法给老鼠找路
        F f1 = new F();
        f1.findway(map, 1, 1);

        System.out.println("找路情况如下");

        for (int i = 0; i < 8; i++) {
    
    
            for (int j = 0; j < 7; j++) {
    
    
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }
}

class F{
    
    

    //使用递归回溯思想写findway方法
    //i,j表示老鼠的位置,最初位置为(1,1)
    //当map[6][5]=2说明找到通路可以结束,否则继续找
    //确定找路策略  下 -> 右 -> 上 -> 左
    public boolean findway(int[][] map , int i , int j){
    
    //找到返回true,没找到返回false

        if(map[6][5] == 2){
    
    //已经找到
            return true;
        }else{
    
    
            if(map[i][j] == 0){
    
    //当前位置为0,可以走
                //假定可以走通
                map[i][j] = 2;
                //开始按策略找路
                if(findway(map, i + 1, j)){
    
    //先向下走
                    return true;
                }else if(findway(map, i, j + 1)){
    
    //向右
                    return true;
                }else if(findway(map, i - 1, j)){
    
    //向上
                    return true;
                }else if(findway(map, i, j - 1)){
    
    //向左
                    return true;
                }else{
    
    //假定可以走通是错误的
                    map[i][j] = 3;
                    return false;
                }
            }else{
    
    //map[i][j]=1,2,3
                return false;
            }
        }
    }
}

The place where the test result
insert image description here
number 2 passes is the path for the little mouse to get out of the maze

3. Summary

The strategy of finding the way is different, and the final path will be different
insert image description here

Guess you like

Origin blog.csdn.net/zcxyywd/article/details/127980936