-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolation.java
149 lines (146 loc) · 3.72 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
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation {
private boolean [] openstate;
private final int sitesN;
private final int top;
private int sitesOpenNum;
private final WeightedQuickUnionUF uf;
public Percolation(int n) // create n-by-n grid, with all sites blocked
{
if (n < 1)
{
throw new java.lang.IllegalArgumentException();
}
sitesN = n;
int total = n * n;
sitesOpenNum = 0;
top = n * n;
uf = new WeightedQuickUnionUF(total + 1);
openstate = new boolean[total + 1];
for (int i = 0; i < total + 1; i++)
{
openstate[i] = false;
}
}
private static void validateIdx(int row, int col, int n)
{
if (row == n * n)
{
return;
}
if (row < 1 || col < 1 || row > n || col > n)
{
throw new java.lang.IllegalArgumentException();
}
}
private int getIdx(int row, int col)
{
return (row - 1) * sitesN + col - 1;
}
public void open(int row, int col) // open site (row, col) if it is not open already
{
validateIdx(row, col, sitesN);
if (isOpen(row, col))
{
return;
}
int ndIdx = getIdx(row, col);
openstate[ndIdx] = true;
sitesOpenNum += 1;
if (row == 1)
{
uf.union(ndIdx, top);
}
else
{
if (isOpen(row - 1, col))
{
int topIdx = getIdx(row - 1, col);
uf.union(ndIdx, topIdx);
}
}
if (row == sitesN)
{
if (isOpen(sitesN, col))
{
int bottomIdx = getIdx(sitesN, col);
uf.union(ndIdx, bottomIdx);
}
}
else
{
if (isOpen(row + 1, col))
{
int bottomIdx = getIdx(row + 1, col);
uf.union(ndIdx, bottomIdx);
}
}
if (col == 1)
{
if (isOpen(row, 1))
{
int leftIdx = getIdx(row, 1);
uf.union(ndIdx, leftIdx);
}
}
else
{
if (isOpen(row, col - 1))
{
int leftIdx = getIdx(row, col - 1);
uf.union(ndIdx, leftIdx);
}
}
if (col == sitesN)
{
if (isOpen(row, sitesN))
{
int rightIdx = getIdx(row, sitesN);
uf.union(ndIdx, rightIdx);
}
}
else
{
if (isOpen(row, col + 1))
{
int rightIdx = getIdx(row, col + 1);
uf.union(ndIdx, rightIdx);
}
}
}
public boolean isOpen(int row, int col) // is site (row, col) open?
{
validateIdx(row, col, sitesN);
int ndIdx = getIdx(row, col);
return openstate[ndIdx];
}
public boolean isFull(int row, int col) // is site (row, col) full?
{
validateIdx(row, col, sitesN);
int ndIdx = getIdx(row, col);
if (uf.connected(ndIdx, top))
{
return true;
}
return false;
}
public int numberOfOpenSites() // number of open sites
{
return sitesOpenNum;
}
public boolean percolates() // does the system percolate?
{
for (int i = 1; i < sitesN + 1; i++)
{
if(!isOpen(sitesN, i))
{
continue;
}
if (isFull(sitesN, i))
{
return true;
}
}
return false;
}
}