廣度優先搜索(Breadth First Search)

廣度優先搜索使用隊列模型

一、把根節點放到隊列的末尾。
二、每次從隊列的頭部取出一個元素,查看這個元素全部的下一級元素,把它們放到隊列的末尾。
並把這個元素記爲它下一級元素的前驅。
三、找到所要找的元素時結束程序。
四、若是遍歷整個樹尚未找到,結束程序。

廣度優先搜索使用棧模型

一、把根節點壓入棧中。
二、每次從棧中彈出一個元素,搜索全部在它下一級的元素,把這些元素壓入棧中。
並把這個元素記爲它下一級元素的前驅。
三、找到所要找的元素時結束程序。
四、若是遍歷整個樹尚未找到,結束程序。

使用棧模型更容易理解,廣度優先搜索,不妨思考一下,棧的運行模式.java

代碼模型

迷宮模型,尋找到達迷宮的最少步數;此代碼使用隊列模型ide

廣度優先搜索

public class Test {

    public static void main(String[] args) {
        Test t = new Test();

        t.bfs();
    }

    private class Node {

        int x;
        int y;
        int step;

        public Node(int x, int y, int step) {
            this.x = x;
            this.y = y;
            this.step = step;
        }
        @Override
        public String toString() {
            return "Node [x=" + x + ", y=" + y + ", step=" + step + "]";
        }
    }

    int startX = 0;
    int startY = 0;
    int[][] plat = new int[][] { { 0, 0, 1, 0, 0 }, { 0, 0, 1, 0, 1 },
               { 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 1 } };
    int p = 2;
    int q = 3;
    int[][] next = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
    int[][] book = new int[4][5];
    Queue<Node> queue = new LinkedList<Node>();

    public int bfs() {

        queue.add(new Node(0, 0, 0));


        while (!queue.isEmpty()) {

            Node newNode = queue.poll();
            book[newNode.x][newNode.y] = 1;
            System.out.println(newNode);
            out(book);
            for (int k = 0; k < 4; k++) {
                int x = newNode.x + next[k][0];
                int y = newNode.y + next[k][1];

                if (x == p && y == q) {
                    System.out.println("至少須要" + (newNode.step + 1));
                    return newNode.step + 1;
                }

                if (x >= 0 && x <= plat.length - 1 && y >= 0 && y <= plat[0].length - 1 && plat[x][y] == 0
                        && book[x][y] == 0) {
                    queue.add(new Node(x, y, newNode.step + 1));
                }

            }

        }
        return -1;
    }

    public void out(int[][] a) {

        for (int[] is : a) {
            for (int i : is) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
        System.out.println();
    }
}

快速理解

隊列出隊進隊.進入[x=0, y=1] step=1,這是從0,0到達0,1的最小步數
一樣到達 [x=1, y=1], step=2
達到目標點[x=2, y=3], step=7

代碼輸出圖

Node [x=0, y=0, step=0]
1 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

Node [x=0, y=1, step=1]
1 1 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

Node [x=1, y=0, step=1]
1 1 0 0 0 
1 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

Node [x=1, y=1, step=2]
1 1 0 0 0 
1 1 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

Node [x=1, y=1, step=2]
1 1 0 0 0 
1 1 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

Node [x=2, y=1, step=3]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
0 0 0 0 0 

Node [x=2, y=1, step=3]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
0 0 0 0 0 

Node [x=3, y=1, step=4]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
0 1 0 0 0 

Node [x=3, y=1, step=4]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
0 1 0 0 0 

Node [x=3, y=2, step=5]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
0 1 1 0 0 

Node [x=3, y=0, step=5]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
1 1 1 0 0 

Node [x=3, y=2, step=5]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
1 1 1 0 0 

Node [x=3, y=0, step=5]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
1 1 1 0 0 

Node [x=3, y=3, step=6]
1 1 0 0 0 
1 1 0 0 0 
0 1 0 0 0 
1 1 1 1 0 

至少須要7
相關文章
相關標籤/搜索