-
Notifications
You must be signed in to change notification settings - Fork 0
/
Percolation.java
121 lines (100 loc) · 3.08 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
//package algorithm;
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
import java.lang.IllegalArgumentException;
//import edu.princeton.cs.algs4.In;
public class Percolation{
private WeightedQuickUnionUF set;
private WeightedQuickUnionUF onlyTop;
private int count; //number of open sites
private boolean[] status;// shows if the site is open
private int N;
private int t_node;
private int b_node;
public Percolation(int number){
count = 0;
N = number;
if (N <= 0) {
throw new IllegalArgumentException("The input N is illegal!");
}
set = new WeightedQuickUnionUF(N*N+2);
onlyTop = new WeightedQuickUnionUF(N*N+1);
status = new boolean[N*N+2];//if open, status = 1; otherwise status = 0;
t_node = 0;//index of the top node;
b_node = N*N+1;//index of the bottom node;
for (int i = 0; i<N*N+2; i++)
{
status[i] = false; //All sites are blocked initially;
}
status[t_node] = true;
status[b_node] = true;
}
public void open(int row, int col)
{
// 1<=row,col<=N
validateArray(row, col);
int index = (row-1)*N + col;
if(status[index]==true) return;
status[index] = true;
count++;
if (row == 1)
{
set.union(t_node,index);
onlyTop.union(t_node,index);
}
if (row == N)
{
set.union(b_node,index);
}
if (row>1 && isOpen(row-1,col))
{
set.union(index,(row-2)*N+col);
onlyTop.union(index,(row-2)*N+col);
}
if (col>1 && isOpen(row,col-1))
{
set.union(index,(row-1)*N+col-1);
onlyTop.union(index,(row-1)*N+col-1);
}
if (col<N && isOpen(row,col+1))
{
set.union(index,(row-1)*N+col+1);
onlyTop.union(index,(row-1)*N+col+1);
}
if (row<N && isOpen(row+1,col))
{
set.union(index,row*N+col);
onlyTop.union(index,row*N+col);
}
}
public boolean isOpen(int row, int col)
{ // input 1<=row,col<=N
validateArray(row, col);
int index = (row-1)*N + col;
return status[index];
}
private void validateArray(int i, int j) {
if (i < 1 || j < 1 || i > N || j > N) {
throw new IllegalArgumentException("index: (" + i + ", " + j + ") are out of bound!");
}
}
public int numberOfOpenSites(){
return count;
}
public boolean isFull(int row, int col){
validateArray(row, col);
int index = (row-1)*N + col;
return onlyTop.connected(t_node, index);
}
public boolean percolates(){
return set.connected(t_node, b_node);
}
public static void main(String[] args)
{
Percolation percolation = new Percolation(3);
percolation.open(1, 3);
percolation.open(2, 3);
percolation.open(3, 3);
percolation.open(3, 1);
System.out.println(percolation.isFull(3,1));
}
}