-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolation.java
96 lines (92 loc) · 2.91 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
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
public class Percolation {
private int n;
private WeightedQuickUnionUF uf;
private boolean[][] open;
// creates n-by-n grid, with all sites initially blocked
public Percolation(int n) {
if (n <= 0) {
throw new IllegalArgumentException();
}
this.n = n;
open = new boolean[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
open[i][j] = false;
}
}
uf = new WeightedQuickUnionUF(n * n + 2);
for(int i = 1; i <= n; i++) {
uf.union(i, 0);
uf.union(n*(n-1) + i, n*n + 1);
}
}
private int getIndex(int row, int col) {
return (row - 1) * n + (col - 1) + 1;
}
// opens the site (row, col) if it is not open already
public void open(int row, int col) {
validate(row, col);
if (!isOpen(row, col)) {
open[row-1][col-1] = true;
if (row == 1) {
uf.union(getIndex(row, col), 0);
}
if (row == n) {
uf.union(getIndex(row, col), n * n + 1);
}
if (row > 1 && isOpen(row-1, col)) {
uf.union(getIndex(row, col), getIndex(row-1, col));
}
if (row < n && isOpen(row+1, col)) {
uf.union(getIndex(row, col), getIndex(row+1, col));
}
if (col > 1 && isOpen(row, col-1)) {
uf.union(getIndex(row, col), getIndex(row, col-1));
}
if (col < n && isOpen(row, col+1)) {
uf.union(getIndex(row, col), getIndex(row, col+1));
}
}
}
// is validate?
private void validate(int row, int col) {
if (row < 1 || row > n || col < 1 || col > n) {
throw new IllegalArgumentException();
}
}
// is the site (row, col) open?
public boolean isOpen(int row, int col) {
validate(row, col);
return open[row-1][col-1];
}
// is the site (row, col) full?
public boolean isFull(int row, int col) {
validate(row, col);
for(int i = 0; i < n; i++) {
if(uf.find(0) == uf.find(i))
uf.union(i, 0);
}
return uf.find(0) == uf.find(getIndex(row, col)) && isOpen(row, col);
}
// returns the number of open sites
public int numberOfOpenSites() {
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (open[i][j]) {
count++;
}
}
}
return count;
}
// does the system percolate?
public boolean percolates() {
return uf.find(0) == uf.find(n * n + 1);
}
}