迷宮路徑求解

主要思路如下:
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;
        }
    }
}

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