Recursive labyrinth of back problems

Recursive labyrinth of back problems

  1. Maze expressed by two-dimensional array
  2. Represent walls and obstacles with 1,
  3. 2 indicates that the use has come a point, after which point, while 2 indicates that the trail is a passage
  4. 3 indicates the point with, passed through, but the trail dead end
  5. Maze is to point a direction of the path selected
    base code:
package maze.migong;

public class Maze {
    public static void main(String[] args) {
        //创建一个二维数组,用来模拟迷宫---地图
        int [][] map = new int [11][12];
        //使用1表示墙,地图四周都是用墙围起来的
        for(int i = 0 ; i < 12;i ++){
            map[0][i] = 1;
            map[10][i] = 1;
        }
        for (int i = 0; i < 11; i++){
            map[i][0] = 1;
            map[i][11] = 1;
        }
        //设置障碍物
        for (int i = 0;i < 4;i ++){
            map[5][i] = 1;
            map[i][8] = 1;
            map[i][6] = 1;
            map[2][i] = 1;
            map[i + 4][5] = 1;
        }
        //输出地图
        for(int i = 0 ;i < 11;i ++){
            for(int j = 0;j < 12;j ++ ){
                System.out.print("  " + map[i][j]);
            }
            System.out.println();
        }
    }

    /**
     *
     * @param map map表示地图
     * @param i
     * @param j  i和j分别表示开始的坐标
     * @return 如果返回值为true表示已经找到路,如果返回false那就是没有找到路
     */

    public abstract boolean setWay(int[][] map , int i , int j){

    }
}

Analysis of individual ideas:
map

My code:

 public static boolean setWay(int[][] map , int i , int j){
        //这个方法的含义是什么?直接找到到达终点的路吗?
        //作为递归方法
        if(map[9][10] == 2){
            return true;
            //到达终点作为递归条件的截止
            //那原来的点都是0,你得有赋值的语句才行
        }else{
            if(map[i][j] == 0){
                //如果你传进来的点没有走过,那就以你传进来的点开始走
                map[i][j] = 2;      //假设该点可以走通,按照下--右--上--左的路径走完
                if(map[i - 1][j] == 0 && setWay(map,i + 1,j)){        //向下走
                    return true;
                }else if(map[i][j + 1] == 0 && setWay(map,i,j + 1)){      //向右走
                    return true;
                }else if(map[i + 1][j] == 0 && setWay(map,i -1,j)){       //向上走
                    return true;
                }else if(map[i][j - 1] == 0 && setWay(map,i,j - 1)){      //向左走
                    return true;
                }else{
                    //说明该点走不通是死路
                    map[i][j] = 3;
                    return false;
                }
            }else if(map[i][j] == 1){
                return false;
            }else if(map[i][j] == 2){
                return false;
            }else{
                return false;
            }
        }
    }
}

The results:
Blind alley
the results of the analysis:
an endless loop, the way it should go down, no choice down.
Problem Solving:
modify
go left to determine whether you can go left, go down below to determine whether you can go wrong decision statement.

The second operation
result
step of FIG.
Step up to see the lower right
Results Analysis:

  1. With trial and error between the invisible function, even a dead end section of track, it will be marked as 3.
  2. Meanwhile here is output in the reverse order of the results, every time he calls another method to determine when the final value is returned to the start of printing
  3. If a wrong route is very long, he opened so many stacks and found a dead end, come back, go back, would not waste a lot of space and time. Every decision must be open so many recursive call stack.
    Course Code:
 public static boolean setWay(int[][] map , int i , int j){
        //这个方法的含义是什么?直接找到到达终点的路吗?
        //作为递归方法
        if(map[9][10] == 2){
            return true;
            //到达终点作为递归条件的截止
            //那原来的点都是0,你得有赋值的语句才行
        }else{
            if(map[i][j] == 0){
                //如果你传进来的点没有走过,那就以你传进来的点开始走
                map[i][j] = 2;      //假设该点可以走通,按照下--右--上--左的路径走完
                if(setWay(map,i - 1,j)){
                    System.out.print("  往上走");//向下走
                    return true;
                }else if(setWay(map,i,j + 1)){
                    System.out.println("  往右走");//向右走
                    return true;
                }else if(setWay(map,i + 1,j)){
                    System.out.print("  往下走");//向上走
                    return true;
                }else if(setWay(map,i,j - 1)){
                    System.out.println("  往左走");//向左走
                    return true;
                }else{
                    //说明该点走不通是死路
                    System.out.println("此路不通");
                    map[i][j] = 3;
                    return false;
                }
            }else{
                //其实只要出现三种情况1、2、3都不可以再走了,所以都是一样的,没必要单列出来
                return false;
            }
        }
    }

A comparative analysis and summary:
1. I think if the conditions are judged at the end of a dead end path, will increase the number of recursive program, every step should be to open a lot of determination conditions recursion, then let them determine what before you go the next step may soon reduce the value of the time you run the program? Although your value is assumed to be 2 at the time, but to determine whether it is to have four directions 2
2. traveled point being the subject of 2, if you choose not to end the way he died, then before being calibrated on all 2 becomes 3, indicating that the chosen path is a dead end
3. direction they can be set in advance, but before walked the streets must not go any farther, how it does not go that?
4 out of recursion conditions should not be only one direction if I started provision is to the left, on the right, then the first line of the first paragraph of this point in the road plunged into an infinite loop.

Published 19 original articles · won praise 3 · Views 567

Guess you like

Origin blog.csdn.net/Blackoutdragon/article/details/103881628