zoukankan      html  css  js  c++  java
  • 305.Number of Islands II

        /*
         * 305.Number of Islands II
         * 2016-4-3 by Mingyang
         *Union Find 的题目,直接略过
         */
         private int[][] dir = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
         public List<Integer> numIslands2(int m, int n, int[][] positions) {
                UnionFind2D islands = new UnionFind2D(m, n);
                List<Integer> ans = new ArrayList<Integer>();
                for (int[] position : positions) {
                    int x = position[0], y = position[1];
                    int p = islands.add(x, y);
                    for (int[] d : dir) {
                        int q = islands.getID(x + d[0], y + d[1]);
                        if (q > 0 && !islands.find(p, q))
                            islands.unite(p, q);
                    }
                    ans.add(islands.size());
                }
                return ans;
            }
    }
    class UnionFind2D {
        private int[] id;
        private int[] sz;
        private int m, n, count;
        public UnionFind2D(int m, int n) {
            this.count = 0;
            this.n = n;
            this.m = m;
            this.id = new int[m * n + 1];
            this.sz = new int[m * n + 1];
        }
        public int index(int x, int y) { return x * n + y + 1; }
        public int size() { return this.count; }
        public int getID(int x, int y) {
            if (0 <= x && x < m && 0<= y && y < n)
                return id[index(x, y)];
            return 0;
        }
        public int add(int x, int y) {
            int i = index(x, y);
            id[i] = i; sz[i] = 1;
            ++count;
            return i;
        }
        public boolean find(int p, int q) {
            return root(p) == root(q);
        }
        public void unite(int p, int q) {
            int i = root(p), j = root(q);
            if (sz[i] < sz[j]) { //weighted quick union
                id[i] = j; sz[j] += sz[i];
            } else {
                id[j] = i; sz[i] += sz[j];
            }
            --count;
        }
        private int root(int i) {
            for (;i != id[i]; i = id[i])
                id[i] = id[id[i]]; //path compression
            return i;
        }
    }
  • 相关阅读:
    Jquery 获取CheckBoxList 中选择的值和获取RadioButton 选中的状态
    JS数组取相同的值和不同的值
    JS删除数组总是报错
    调用WebAPI接口
    MVC3 Excel导入数据到sql Server数据库
    MVC JS动态添加 @Html.DropDownList
    得瑟一下
    出差小记
    jQuery自学打卡二
    jQuery自学打卡一
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5639534.html
Copyright © 2011-2022 走看看