-
Notifications
You must be signed in to change notification settings - Fork 0
/
Percolation.java
executable file
·99 lines (95 loc) · 3.13 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
public class Percolation {
private int size;
// create N-by-N grid, with all sites blocked
private boolean [][] grid = null;
private WeightedQuickUnionUF open = null;
private WeightedQuickUnionUF full = null;
private int top;
private int bottom ;
public Percolation(int N) {
if (N <= 0) {throw new IllegalArgumentException();}
this.size = N;
this.top = 0;
this.bottom = N * N + 1;
grid = new boolean[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
grid[i][j] = false;
}
}
open = new WeightedQuickUnionUF(size * size + 2);
full = new WeightedQuickUnionUF(size * size + 2);
}
private boolean valid(int i, int j) {
boolean result = false;
if (i < 1 || j < 1 || i > size || j > size) {throw new IndexOutOfBoundsException(); }
result = true;
return result;
}
//map from a 2-dimensional (row, column) pair to a 1-dimensional union find object index
private int xyto1d(int i, int j) {
return (i - 1) * size + j;
}
// open site (row i, column j) if it is not open already
public void open(int i, int j) {
if (valid(i, j)) {
grid[i - 1][j - 1] = true;
if (i == 1) {
open.union(top, xyto1d(i, j));
full.union(top, xyto1d(i, j));
}
else {
if (grid[i - 2][j - 1]) {
open.union(xyto1d(i - 1,j),xyto1d(i, j));
full.union(xyto1d(i - 1,j),xyto1d(i, j));
}
}
if (i == size) {
open.union(bottom, xyto1d(i, j));
}
else {
if (grid[i][j - 1]) {
open.union(xyto1d(i + 1,j),xyto1d(i, j));
full.union(xyto1d(i + 1,j),xyto1d(i, j));
}
}
if (j > 1) {
if (grid[i - 1][j - 2]){
open.union(xyto1d(i,j - 1), xyto1d(i, j));
full.union(xyto1d(i,j - 1), xyto1d(i, j));
}
}
if (j < size){
if (grid[i - 1][j]){
open.union(xyto1d(i, j + 1), xyto1d(i, j));
full.union(xyto1d(i, j + 1), xyto1d(i, j));
}
}
// if (valid(i-1,j) && grid[i-2][j-1] == true) {union.union(xyto1d(i-1, j),xyto1d(i, j));}
// if (valid(i+1,j )&& grid[i][j-1] == true) {union.union(xyto1d(i+1, j),xyto1d(i, j));}
// if (valid (i,j-1) && grid[i-1][j-2] == true ) {union.union(xyto1d(i, j-1),xyto1d(i, j));}
// if (valid (i,j+1) && grid[i-1][j] == true) {union.union(xyto1d(i, j+1),xyto1d(i, j));}
}
}
// is site (row i, column j) open?
public boolean isOpen(int i, int j) {
return grid[i - 1][j - 1] == true;
}
// is site (row i, column j) full?
public boolean isFull(int i, int j) {
return valid(i , j) && grid [i - 1][j - 1] && full.connected(top, xyto1d(i, j));
}
// does the system percolate?
public boolean percolates() {
return open.connected(top, bottom);
}
// test client (optional)
public static void main(String[] args) {
Percolation test = new Percolation(4);
test.open(1, 1);
test.open(1, 2);
// System.out.print(test.isOpen(0, 1));
boolean test1 = test.full.connected(1, 2);
System.out.print(test1);
}
}