-
Notifications
You must be signed in to change notification settings - Fork 0
/
Percolation.java
87 lines (74 loc) · 2.52 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
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation {
private static final int TOP = 0;
private final int bottom;
private int numberofOpenSites;
private final boolean[][] opened; // stores opended
private final WeightedQuickUnionUF qf;
private final int size;
// creates n-by-n grid, with all sites initially blocked
public Percolation(int n) {
if (n < 0)
throw new IllegalArgumentException();
size = n;
bottom = size * size + 1;
qf = new WeightedQuickUnionUF(size * size + 2);
opened = new boolean[size + 1][size + 1];
numberofOpenSites = 0;
}
// opens the site (row, col) if it is not open already
public void open(int row, int col) {
int index;
checkValidity(row, col);
index = getPosition(row, col);
opened[row][col] = true;
numberofOpenSites++;
if (row == 1) {
qf.union(index, TOP);
}
if (row == size) {
qf.union(index, bottom);
}
if (row > 1 && isOpen(row - 1, col))
qf.union(index, getPosition(row - 1, col));
if (row < size && isOpen(row + 1, col))
qf.union(index, getPosition(row + 1, col));
if (col < size && isOpen(row, col + 1))
qf.union(index, getPosition(row, col + 1));
if (col > 1 && isOpen(row, col - 1))
qf.union(index, getPosition(row, col - 1));
}
// return index
private int getPosition(int row, int col) {
return size * (row - 1) + col;
}
// is the site (row, col) open?
public boolean isOpen(int row, int col) {
checkValidity(row, col);
return opened[row][col];
}
// check Valid row col index
private void checkValidity(int row, int col) {
if ((row <= 0 || row > size) || (col <= 0 || col > size)) {
throw new IllegalArgumentException();
}
}
// is the site (row, col) full?
public boolean isFull(int row, int col) {
if ((row > 0 && row <= size) && (col > 0 && col <= size)) {
return (qf.find(TOP) == qf.find(getPosition(row, col)));
}
else throw new IllegalArgumentException();
}
// returns the number of open sites
public int numberOfOpenSites() {
return numberofOpenSites;
}
// does the system percolate?
public boolean percolates() {
return qf.find(TOP) == qf.find(bottom);
}
/* test client (optional)
public static void main(String[] args) {
}*/
}