-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolation.java
206 lines (181 loc) · 6.03 KB
/
Percolation.java
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation {
private boolean grid[];
private int openSites;
private int grid_size;
private WeightedQuickUnionUF weightedQuickUnionUF;
// creates n-by-n grid, with all sites initially blocked
public Percolation(int n) {
this.validate(n);
this.grid = new boolean[n * n];
this.openSites = 0;
this.grid_size = n;
this.weightedQuickUnionUF = new WeightedQuickUnionUF(n * n + 2);
for (int i = 0; i < grid_size; i++) {
weightedQuickUnionUF.union(n * n , i);
}
for (int i = 0; i < grid_size; i++) {
weightedQuickUnionUF.union(n * n + 1, getCoordinates(grid_size, i + 1));
}
}
// opens the site (row, col) if it is not open already
public void open(int row, int col) {
this.validate(row, col);
if (isOpen(row, col))
return;
this.grid[getCoordinates(row, col)] = true;
this.openSites++;
this.checkNeighbourhood(row, col);
}
private void checkNeighbourhood(int row, int col) {
// edge case of first row
if (row == 1) {
// check bottom cell
if (grid[getCoordinates(row + 1, col)] == true) {
weightedQuickUnionUF.union(getCoordinates(row + 1, col), getCoordinates(row, col));
}
return;
}
// edge case of last row
else if (row == grid_size) {
// check top cell
if (grid[getCoordinates(row - 1, col)] == true) {
weightedQuickUnionUF.union(getCoordinates(row, col), getCoordinates(row - 1, col));
//checkPercolated(row, col);
//weightedQuickUnionUF.union(grid_size * grid_size - 1, getCoordinates(row - 1, col));
}
}
// edge case of first col
else if (col == 1) {
// check right
if (grid[getCoordinates(row, col + 1)] == true) {
weightedQuickUnionUF.union(getCoordinates(row, col), getCoordinates(row, col + 1));
//checkPercolated(row, col);
}
// check bottom cell
if (grid[getCoordinates(row + 1, col)] == true) {
weightedQuickUnionUF.union(getCoordinates(row + 1, col), getCoordinates(row, col));
//checkPercolated(row, col);
}
// check top cell
if (grid[getCoordinates(row - 1, col)] == true) {
weightedQuickUnionUF.union(getCoordinates(row, col), getCoordinates(row - 1, col));
//checkPercolated(row, col);
}
}
// edge case of last col
else if (col == grid_size) {
// check left cell
if (grid[getCoordinates(row, col - 1)] == true) {
weightedQuickUnionUF.union(getCoordinates(row, col), getCoordinates(row, col - 1));
//checkPercolated(row, col);
}
// check bottom cell
if (grid[getCoordinates(row + 1, col)] == true) {
weightedQuickUnionUF.union(getCoordinates(row + 1, col), getCoordinates(row, col));
//checkPercolated(row, col);
}
// check top cell
if (grid[getCoordinates(row - 1, col)] == true) {
weightedQuickUnionUF.union(getCoordinates(row, col), getCoordinates(row - 1, col));
//checkPercolated(row, col);
}
} else {
// check right
if (grid[getCoordinates(row, col + 1)] == true) {
weightedQuickUnionUF.union(getCoordinates(row, col), getCoordinates(row, col + 1));
//checkPercolated(row, col);
}
// check left cell
if (grid[getCoordinates(row, col - 1)] == true) {
weightedQuickUnionUF.union(getCoordinates(row, col), getCoordinates(row, col - 1));
//checkPercolated(row, col);
}
// check bottom cell
if (grid[getCoordinates(row + 1, col)] == true) {
weightedQuickUnionUF.union(getCoordinates(row + 1, col), getCoordinates(row, col));
//checkPercolated(row, col);
}
// check top cell
if (grid[getCoordinates(row - 1, col)] == true) {
weightedQuickUnionUF.union(getCoordinates(row, col), getCoordinates(row - 1, col));
//checkPercolated(row, col);
}
}
}
/*private void checkPercolated(int row, int col) {
if (weightedQuickUnionUF.find(getCoordinates(row, col)) < grid_size) {
percolated = true;
}
if (weightedQuickUnionUF.find(getCoordinates(row, col)) == grid_size * grid_size) {
percolated = true;
}
}*/
// handles 2D 1 based coordinates to 0 based array notation
private int getCoordinates(int row, int col) {
return (grid_size * (row - 1)) + col - 1;
}
// does the system percolate?
public boolean percolates() {
int headPtr = weightedQuickUnionUF.find(grid_size * grid_size);
int tailPtr = weightedQuickUnionUF.find(grid_size * grid_size + 1);
return headPtr == tailPtr;
}
// is the site (row, col) open?
public boolean isOpen(int row, int col) {
this.validate(row, col);
return this.grid[getCoordinates(row, col)];
}
// is the site (row, col) full?
public boolean isFull(int row, int col) {
if (!isOpen(row, col))
return false;
int curPtr = weightedQuickUnionUF.find(getCoordinates(row, col));
int headPtr = weightedQuickUnionUF.find(grid_size * grid_size);
return curPtr == headPtr;
}
// returns the number of open sites
public int numberOfOpenSites() {
return this.openSites;
}
// test client (optional)
public static void main(String[] args) {
}
@Override
public String toString() {
String ret = "Percolation [grid=\n";
for (int i = 0; i < grid_size; i++) {
for (int j = 0; j < grid_size; j++) {
if (grid[i * grid_size + j])
ret += " " + 0;
else
ret += " " + 1;
}
ret += "\n";
}
ret += ", openSites=" + openSites + ", n=" + grid_size + "]";
ret += "\n";
for (int i = 0; i < grid_size; i++) {
for (int j = 0; j < grid_size; j++) {
ret += " " + weightedQuickUnionUF.find(i * grid_size + j);
}
ret += "\n";
}
ret += " " + weightedQuickUnionUF.find(grid_size * grid_size);
ret += " " + weightedQuickUnionUF.find(grid_size * grid_size + 1);
return ret;
}
private void validate(int n) {
if (n <= 0) {
throw new IllegalArgumentException();
}
}
private void validate(int row, int col) {
if (row > this.grid_size || row < 1) {
throw new IllegalArgumentException();
}
if (col > this.grid_size || col < 1) {
throw new IllegalArgumentException();
}
}
}