-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolation
More file actions
58 lines (52 loc) · 1.53 KB
/
Percolation
File metadata and controls
58 lines (52 loc) · 1.53 KB
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
public class Percolation {
int openedCount; //к-ть відкритих комірок (1)
public WeightedQuickUnionUF wquf;
int N; // кількість рядків (і стовбців) у квадраті
int[][] id; //квадрат з нулями
public Percolation(int N) {
this.N = N;
this.id = new int[N][N];
this.wquf = new WeightedQuickUnionUF(N*N);
for(int i = 0; i<N; i++){
wquf.union(i, 0);
}
for(int i=N*N-N; i<N*N; i++){
wquf.union(i, N*N-1);
}
}
public int getOpenedCount() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (isOpened(i, j)) {
openedCount++;
}
}
}
return openedCount;
}
public void open(int i, int j){
if(!isOpened(i, j)){
id[i][j] = 1;
if(i!=0 && isOpened(i-1, j))
wquf.union(i*N+j, (i-1)*N+j);
if(j!=0 && isOpened(i, j-1))
wquf.union(i*N+j, i*N+j-1);
if(i!=N-1 && isOpened(i+1, j))
wquf.union(i*N+j, (i+1)*N+j);
if(j!=N-1 && isOpened(i, j+1))
wquf.union(i*N+j, i*N+j+1);
}
}
public boolean isOpened(int i, int j) {
if (id[i][j] == 1) {
return true;
}
return false;
}
public boolean percolates() {
if (wquf.connected(0, N*N-1)) {
return true;
}
return false;
}
}