迷宫路径求解

主要思路如下:
1、入口区域入栈
2、判断栈顶区域是不是出口
3、如果是则输出栈内全部元素,这就是一个从入口到出口的逆序路线
4、如果不是则探索栈顶区域的四个方向
5、如果这四个方向均不能走(相邻的区域是墙、探索过不能同行、)已经在路径中,则把这块区域从路经栈中删除,然后执行步骤2
6、如果这块区域的相邻区域存在可通行的区域,则将第一个可通行的区域入栈,然后执行步骤2
ps:这时练习栈的知识点所作的练习,所以求出来的路经不一定是最优路径,需要最优路径的不需要向下看了

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;
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章