forked from andrewfwalters/CSC2053_Project1_Percolation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Percolation.java
110 lines (90 loc) · 2.96 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
/*
* Compilation: javac Percolation.java
* Execution: This class does not contain a main method
* Dependancies: WeightedQuickUnionUF.java
*
* This class creates an N by N grid of closed sites
* that can be opened by the function open(row,col).
* The function isFull(row,col) will determine whether
* water has percolated to that site and percolates() will
* determine if water has percolated to through the grid.
*
* grid indicies begin at 1 instead of zero for all function in this class
*/
public class Percolation {
//constants
private final int CLOSED = 0;
private final int OPEN = 1;
//variables
private int size;
private int grid[][];
private WeightedQuickUnionUF perc;
private WeightedQuickUnionUF full;
// create N-by-N grid, with all sites blocked
public Percolation(int N) {
perc = new WeightedQuickUnionUF(N*N+2);
full = new WeightedQuickUnionUF(N*N+1);
grid = new int[N][N];
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
grid[i][j] = CLOSED;
}//end for
}//end for
size = N;
}//end constructor
// open site (row i, column j) if it is not already
public void open(int i, int j) {
if(isOpen(i,j)) {
return;
}
else {
grid[(i-1)][(j-1)] = OPEN;
//join left
if(j!=1)
con(i,j,i,(j-1));
//join right
if(j!=size)
con(i,j,i,(j+1));
//join above
if(i!=1) {
con(i,j,(i-1),j);
}
else {
perc.union(g2p(i,j),0);
full.union(g2p(i,j),0);
}
//join below
if(i!=size)
con(i,j,(i+1),j);
else
perc.union(g2p(i,j),(size*size+1));
}//end else
}//end open
// is site (row i, column j) open?
public boolean isOpen(int i, int j) {
if(grid[(i-1)][(j-1)] == OPEN)
return true;
else
return false;
}//end isOpen
// is site (row i, column j) full?
public boolean isFull(int i, int j) {
return full.connected(0,g2p(i,j));
}//end isFull
// does the system percolate?
public boolean percolates() {
return perc.connected(0,size*size+1);
}//end percolates
//converts grid coordinates to index of UF struct
private int g2p(int i, int j){
return (i-1)*size + (j-1) + 1;
}//end mod
//union two sites
private void con(int pi, int pj, int qi, int qj) {
//ensure both sites are open
if( isOpen(pi,pj) && isOpen(qi,qj) ) {
perc.union(g2p(pi,pj),g2p(qi,qj));
full.union(g2p(pi,pj),g2p(qi,qj));
}//end if
}//end con
}//end class