1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| package UnionFind;
import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch;
public class Application {
private static class Node { int i; int j; int value;
public Node(int i, int j, int value) { this.i = i; this.j = j; this.value = value; } }
private static Node[][] nodes; private static UnionFind.UnionFindSet<Node> unionFindSet;
public static int countIslandsUnionFind(int[][] m) throws InterruptedException { if (m == null || m[0] == null) { return 0; } int N = m.length; int M = m[0].length; final int[] results = {0, 0}; List<Node> list = new ArrayList<>(); nodes = new Node[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { Node node = new Node(i, j, m[i][j]); nodes[i][j] = node; if (m[i][j] == 1) { list.add(node); } } } unionFindSet = new UnionFind.UnionFindSet<Node>(list); final CountDownLatch latch = new CountDownLatch(2); Thread t1 = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < N; i++) { for (int j = 0; j < M / 2; j++) { if (nodes[i][j].value == 1) { results[0]++; infectUnionFind(i, j, 0, N, 0, M / 2); } } } latch.countDown(); } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < N; i++) { for (int j = M / 2; j < M; j++) { if (nodes[i][j].value == 1) { results[1]++; infectUnionFind(i, j, 0, N, M / 2, M); } } } latch.countDown(); } }); t1.start(); t2.start(); latch.await(); int result = results[0] + results[1]; for (int i = 0; i < N; i++) { if (nodes[i][M / 2 - 1].value == nodes[i][M / 2].value && nodes[i][M / 2 - 1].value == 2 && !unionFindSet.isSameSet(nodes[i][M / 2 - 1], nodes[i][M / 2])) { unionFindSet.union(nodes[i][M / 2 - 1], nodes[i][M / 2]); result--; } } return result; }
private static boolean infectUnionFind(int i, int j, int N1, int N2, int M1, int M2) { if (i < N1 || i >= N2 || j < M1 || j >= M2 || nodes[i][j].value != 1) { return false; } nodes[i][j].value = 2; if (infectUnionFind(i + 1, j, N1, N2, M1, M2)) { unionFindSet.union(nodes[i][j], nodes[i + 1][j]); } if (infectUnionFind(i - 1, j, N1, N2, M1, M2)) { unionFindSet.union(nodes[i][j], nodes[i - 1][j]); } if (infectUnionFind(i, j + 1, N1, N2, M1, M2)) { unionFindSet.union(nodes[i][j], nodes[i][j + 1]); } if (infectUnionFind(i, j - 1, N1, N2, M1, M2)) { unionFindSet.union(nodes[i][j], nodes[i][j - 1]); } return true; }
public static int countIslands(int[][] m) { if (m == null || m[0] == null) { return 0; } int N = m.length; int M = m[0].length; int result = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (m[i][j] == 1) { result++; infect(m, i, j, N, M); } } } return result; }
private static void infect(int[][] m, int i, int j, int N, int M) { if (i < 0 || i >= N || j < 0 || j >= M || m[i][j] != 1) { return; } m[i][j] = 2; infect(m, i + 1, j, N, M); infect(m, i - 1, j, N, M); infect(m, i, j + 1, N, M); infect(m, i, j - 1, N, M); } }
|