Solving the maze path

The main idea is as follows:
1, the stack inlet region
2, the region is not judged stack outlet
3, if the outputs of all elements in the stack, which is a reverse path from the inlet to the outlet
4, if it is not to explore the top of the stack area four directions
5, if the four directions can not take (the area adjacent a wall, explored traveling together) already, put this area is removed from the stack path in the path, and then perform step 2
6 If this area of the adjacent area there is passable area, it will be the first passable area of the stack, and then perform step 2
PS: practice then practice the knowledge stack made, so do not seek out the path must be the optimal path requires optimal path does not need to read down

package stact;

public class MazePath {

    /*
    * 迷宫路径求解
    * */
    public static void main(String[] args) {
        int [][]maze = {
                {0,0,0,0,0,0,0,0,0,0},
                {0,1,1,0,1,1,1,0,1,0},
                {0,1,1,0,1,1,1,0,1,0},
                {0,1,1,1,1,0,0,1,1,0},
                {0,1,0,0,0,1,1,1,1,0},
                {0,1,1,1,0,1,1,1,1,0},
                {0,1,0,1,1,1,0,1,1,0},
                {0,1,0,0,0,1,0,0,1,0},
                {0,0,1,1,1,1,1,1,1,0},
                {0,0,0,0,0,0,0,0,0,0}};  //迷宫,0代表墙体,1代表可通行
        Block in = new Block(8,8,1);   //入口
        Block out = new Block(1,1,1);  //出口
        Stact<Block>  path = new Stact<>();          //路径栈
        /*
        * 1、入口区域入栈
        * 2、判断栈顶区域是不是出口
        * 3、如果是则输出栈内全部元素,这就是一个从入口到出口的逆序路线
        * 4、如果不是则探索栈顶区域的四个方向
        * 5、如果这四个方向均不能走(相邻的区域是墙、探索过不能同行、)已经在路径中,则把这块区域从路经栈中删除,然后执行步骤2
        * 6、如果这块区域的相邻区域存在可通行的区域,则将第一个可通行的区域入栈,然后执行步骤2
        * */
        path.push(in);
        Block top = path.top();
        while (!(top.x == out.x && top.y == out.y)) {
            int x = top.x; int y = top.y;
            if (maze[x][y -1] == 1 ) { //当前栈顶区域的左边区域可通行
                path.push(new Block(x,y - 1,3));
                maze[x][y - 1] = 3;
            } else { //当前栈顶区域的左边区域不可通行
                if (maze[x + 1][y] == 1) {  //下
                    path.push(new Block(x + 1,y,3));
                    maze[x + 1][y] = 3;
                } else {
                    if (maze[x][y + 1] == 1) {  //右
                        path.push(new Block(x,y + 1,3));
                        maze[x][y + 1] = 3;
                    } else {
                        if (maze[x - 1][y] == 1) {  //上
                            path.push(new Block(x - 1,y,3));
                            maze[x - 1][y] = 3;
                        } else {  //四个方向均不可通行
                            path.pop();
                            maze[x][y] = 2;
                        }
                    }
                }
            }
            top = path.top();
        }
        while (path.base != path.top) {
            Block b = path.pop();
            System.out.println("[" + b.x + "," + b.y + "]");
        }
    }
    static class Block {
        int x;
        int y;  //x、y代表本块区域的坐标
        int status; //代表本块区域的状态,主要有 0:墙体,1:可通行,2:探索完成不能到达终点的区域,3:已纳入为路径的部分
        public Block(int x,int y,int status) {
            this.x = x;
            this.y = y;
            this.status = status;
        }
    }
    static class Stact<E> {
         int base;
         int top;

        Object []notes = new Object[10000];

        public Stact() {
            this.top = 0;
            this.base = 0;
        }

        /*
         * 删除栈顶元素并返回
         * */
        public E pop() {
            return (E)notes[-- top];
        }

        /*
         * top获取栈顶元素
         * */
        public E top() {
            return (E)notes[top - 1];
        }

        /*
         * 向栈中添加一个元素
         * */
        public void push(E e) {
            notes[top ++] = e;
        }
    }
}

Published 57 original articles · won praise 55 · views 1949

Guess you like

Origin blog.csdn.net/qq_40561126/article/details/104283892