-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolation.java
More file actions
141 lines (136 loc) · 4.37 KB
/
Percolation.java
File metadata and controls
141 lines (136 loc) · 4.37 KB
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
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation {
// Private variables
private final WeightedQuickUnionUF uf;
private boolean[][] mat;
private boolean[][] isConToBot;
private boolean isRootConToBot;
private final short n;
private int openSites;
// Constructor
public Percolation(int enn) {
if (enn <= 0) throw new IllegalArgumentException("Out of Bounds!");
n = (short) enn;
uf = new WeightedQuickUnionUF((n*n) + 1);
mat = new boolean[n][n];
isConToBot = new boolean[n][n];
openSites = 0;
}
// Private Methods
private int get1d(short i, short j) {
return ((int) i * (int) n) + j + 1;
}
private short[] get2d(int a) {
short i = (short) ((a - 1) / n);
short j = (short) ((a - 1) - (i * n));
short[] ret = {i, j};
return ret;
}
private short[][] getNeighbours(short i, short j) {
byte count = 0, size = 4;
if (((i == 0) || (i == (n - 1))) && ((j == 0) || (j == (n - 1)))) size = 2; // if (i, j) is at corners
else if ((i == 0) || (i == (n - 1)) || (j == 0) || (j == (n - 1))) size = 3; // if (i, j) is at sides
short[][] ret = new short[size][2];
if (!(i == 0)) {
ret[count][0] = (short) (i - 1);
ret[count][1] = j;
count++;
}
if (!(i == (n - 1))) {
ret[count][0] = (short) (i + 1);
ret[count][1] = j;
count++;
}
if (!(j == 0)) {
ret[count][0] = i;
ret[count][1] = (short) (j - 1);
count++;
}
if (!(j == (n - 1))) {
ret[count][0] = i;
ret[count][1] = (short) (j + 1);
}
return ret;
}
private boolean getConToBot(short i, short j) {
int root = uf.find(get1d(i, j));
if (root == 0) {
return isRootConToBot;
} else {
short[] root2d = get2d(root);
return isConToBot[root2d[0]][root2d[1]];
}
}
private void setConToBot(short i, short j) {
int root = uf.find(get1d(i, j));
if (root == 0) {
isRootConToBot = true;
} else {
short[] root2d = get2d(root);
isConToBot[root2d[0]][root2d[1]] = true;
}
}
private void unite(short i, short j) {
short[][] neigh = getNeighbours(i, j);
boolean changeIsConToBot = false;
if (i == 0) {
uf.union(get1d(i, j), 0); // if at top, union with first
}
if (i == (n - 1)) changeIsConToBot = true;
for (byte a = 0; a < neigh.length; a++) {
short x = neigh[a][0];
short y = neigh[a][1];
if (mat[x][y]) {
if (!isRootConToBot && !changeIsConToBot) changeIsConToBot = getConToBot(x, y);
uf.union(get1d(i, j), get1d(x, y));
}
}
if (!isRootConToBot && changeIsConToBot) {
setConToBot(i, j);
}
}
private void openPriv(short i, short j) {
if (!mat[i][j]) {
++openSites;
mat[i][j] = true;
unite(i, j);
}
}
private boolean isFullPriv(short i, short j) {
if (mat[i][j]) {
return uf.connected(get1d(i, j), 0);
} else return false;
}
private void excep(int i, int j) {
if (i < 1 || i > n || j < 1 || j > n) throw new IllegalArgumentException("Out of Bounds!");
}
// Public Methods
public void open(int i, int j) {
excep(i, j);
openPriv((short) --i, (short) --j);
}
public boolean isOpen(int i, int j) {
excep(i, j);
return mat[--i][--j];
}
public boolean isFull(int i, int j) {
excep(i, j);
return isFullPriv((short) --i, (short) --j);
}
public int numberOfOpenSites() {
return openSites;
}
public boolean percolates() {
if (isRootConToBot) {
return true;
} else {
int root = uf.find(0);
if (root == 0) {
return false;
} else {
short[] root2d = get2d(root);
return isConToBot[root2d[0]][root2d[1]];
}
}
}
}