-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolation.java
117 lines (97 loc) · 3.21 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
import edu.princeton.cs.algs4.StdRandom;
//import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation {
private int[] Open_node;
private int N;
private WeightedQuickUnionUF UF;
public Percolation(int n) // create n-by-n grid, with all sites blocked
{
N = n;
Open_node = new int[N * N + 2];
for(int i = 1; i <= N * N; i++)
{
Open_node[i] = 0; // all blocked
}
Open_node[0] = 1;
Open_node[N * N + 1] = 1;
UF = new WeightedQuickUnionUF(N * N + 2); // including top root and bottom root
}
public void open(int i, int j) // open site (row i, column j) if it is not open already
{
if(isOpen(i, j) == false)
{
Open_node[(i - 1) * N + j] = 1;
if(i == 1)
{
UF.union(0, j);
}
else if(i == N)
{
UF.union(N * N + 1, (i - 1) * N + j);
}
if(isOpen(i - 1, j) == true)
{
UF.union((i - 1) * N + j, (i - 2) * N + j);
}
if(isOpen(i + 1, j) == true)
{
UF.union((i - 1) * N + j, i * N + j);
}
if(isOpen(i, j - 1) == true)
{
UF.union((i - 1) * N + j, (i - 1) * N + j - 1);
}
if(isOpen(i, j + 1) == true)
{
UF.union((i - 1) * N + j, (i - 1) * N + j + 1);
}
}
}
public boolean isOpen(int i, int j) // is site (row i, column j) open?
{
if((i == 0)||(i == N + 1)||(j == 0)||(j == N + 1))
{
return false;
}
else if(Open_node[(i - 1) * N + j] == 1)
{
return true;
}
else{
return false;
}
}
public boolean isFull(int i, int j) // is site (row i, column j) full?
{
return UF.connected(0, (i - 1) * N + j); // use 0 to represent top root
}
public boolean percolates() // does the system percolate?
{
return isFull(N, N+1);
}
public static void main(String[] args) // test client (optional)
{
Percolation P = new Percolation(10);
int open_site = 0;
double T;
int random_N;
//random_N = 86;
//System.out.format("%d\ni:%d j:%d\n", random_N, (int)(random_N / P.N)+1, (int)(random_N % 10));
while(P.percolates() == false)
{
open_site++;
if(open_site > P.N * P.N){break;}
do{
random_N = (int)(StdRandom.random() * P.N * P.N);
//random_N = 4;
}while(random_N == 0);
//random_N = (open_site - 1) * P.N + 1; //vertical drill
System.out.format("%d\ni:%d j:%d counter:%d\n", random_N, (int)(random_N / P.N) + 1, (int)(random_N % P.N), open_site);
P.open((int)(random_N / P.N) + 1, (int)(random_N % P.N));
}
T = open_site / (double)(P.N * P.N);
//T = StdRandom.random();
System.out.format("\n%f\n", T);
}
}