编程之美1.14连连看游戏设计-----广度优先算法Java版

代码从网上找的,稍微改了改。
第一遍学习先学到这些吧
public class BFS {

    public static class Queue {

        public int Depth;
        public int Dot;

        Queue() {
            Depth = -1;
            Dot = -1;
        }

        public void enterQueue(int dot, int dep) {
            Dot = dot;
            Depth = dep;
            System.out.println(Dot + " " + "The Depth is:" + Depth);
        }

        public int depDate() {
            return Depth;
        }

        public int dotdate() {
            return Dot;
        }
    };

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[][] Graph = {{0, 1, 0, 0, 1, 0, 0, 0, 0, 0},
        {1, 0, 1, 1, 0, 0, 0, 0, 0, 0},
        {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
        {0, 1, 0, 0, 1, 1, 0, 0, 0, 0},
        {1, 0, 0, 1, 0, 0, 0, 0, 1, 0},
        {0, 0, 0, 1, 0, 0, 1, 1, 0, 0},
        {0, 0, 1, 0, 0, 1, 0, 0, 0, 1},
        {0, 0, 0, 0, 0, 1, 0, 0, 1, 1},
        {0, 0, 0, 0, 1, 0, 0, 1, 0, 0},
        {0, 0, 0, 0, 0, 0, 1, 1, 0, 0},};
        boolean[] vis = new boolean[Graph.length];
        Arrays.fill(vis, false);

        Queue[] queue = new Queue[Graph.length];
        for (int i = 0; i < 10; i++) {
            queue[i] = new Queue();
        }
       BFS1(queue, Graph, vis);
        
    }

    /*
     可以实现一个起点无法遍历全部节点时的情况
     */

    public static void BFS1(Queue[] queue, int[][] Graph, boolean[] vis) {
        int base = 0;
        int top = 0;
        for (int i = 0; i < Graph.length; i++) {
            if (vis[i] == false) {
                queue[top].enterQueue(i, 1);
                top++;
                vis[i] = true;
                while (top != base) {
                   int dep = queue[base].depDate() + 1;
                   int dot = queue[base].dotdate();
                    for (int j = 0; j < 10; j++) {
                        if (Graph[dot][j] == 1 && vis[j] == false) {
                            queue[top].enterQueue(j, dep);
                            top++;
                            vis[j] = true;
                        }
                    }
                    base++;
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值