-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolation.java
More file actions
141 lines (107 loc) · 3.91 KB
/
Percolation.java
File metadata and controls
141 lines (107 loc) · 3.91 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
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
/* *****************************************************************************
* Name: Spandan Mishra
* Date: 10th Mar'19
* Description: Percolation API
**************************************************************************** */
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation {
private final int n;
private boolean[][] grid;
private final WeightedQuickUnionUF qu;
private final int virtualTop;
private final int virtualBot;
private int numberOfOpenSites;
public Percolation(int n) {
if (n <= 0) throw new IllegalArgumentException();
this.n = n;
this.grid = new boolean[n][n]; // initially false i.e. no open sites
int maxIndex = xyMapper(n, n);
virtualTop = maxIndex + 1;
virtualBot = maxIndex + 2;
// System.out.println("TOP = " + virtualTop + " BOT = " + virtualBot);
qu = new WeightedQuickUnionUF(virtualBot + 1); // as O based arr Index
}
private int xyMapper(int x, int y) {
return (x - 1) * n + (y - 1);
}
// Since I am using 0 based index -> won't it cause any problem?
// I think it won't matter now as I am decrementing x and y before mapping
public boolean isOpen(int x, int y) {
isValidIndex(x - 1, y - 1);
return grid[x - 1][y - 1];
}
// Checks if the given site is connected to Virtual TOP or not
public boolean isFull(int x, int y) {
isValidIndex(x - 1, y - 1);
return qu.connected(virtualTop, xyMapper(x, y));
}
public void open(int x, int y) {
isValidIndex(x - 1, y - 1);
if (!isOpen(x, y)) numberOfOpenSites++;
grid[x - 1][y - 1] = true;
int currentSite = xyMapper(x, y);
// left edge check
if (!isLowerBoundary(y - 1)) {
// System.out.println("if 1 " + xyMapper(x, y - 1));
if (isOpen(x, y - 1))
qu.union(currentSite, xyMapper(x, y - 1));
}
// right edge check
if (!isUpperBoundary(y - 1)) {
if (isOpen(x, y + 1))
// System.out.println("if 2 " + xyMapper(x, y + 1));
qu.union(currentSite, xyMapper(x, y + 1));
}
// top edge check
if (!isLowerBoundary(x - 1)) {
if (isOpen(x - 1, y))
// System.out.println("if 3 " + xyMapper(x - 1, y));
qu.union(currentSite, xyMapper(x - 1, y));
}
else {
qu.union(currentSite, virtualTop);
}
// bottom edge check
if (!isUpperBoundary(x - 1)) {
if (isOpen(x + 1, y))
// System.out.println("if 4 " + xyMapper(x + 1, y));
qu.union(currentSite, xyMapper(x + 1, y));
}
else {
qu.union(currentSite, virtualBot);
}
}
public boolean percolates() {
return qu.connected(virtualTop, virtualBot);
}
public int numberOfOpenSites() {
return numberOfOpenSites;
}
private void isValidIndex(int x, int y) {
if (x < 0 && x >= n && y < 0 && y >= n)
throw new IllegalArgumentException();
}
// boundary checks
private boolean isLowerBoundary(int i) {
return i == 0;
}
private boolean isUpperBoundary(int i) {
return i == n - 1;
}
public static void main(String[] args) {
// Percolation p = new Percolation(3);
// System.out.println(p.isOpen(0, 0));
// System.out.println(p.isOpen(1, 1));
// p.open(2, 2);
// p.open(2, 1);
// p.open(2, 2);
// System.out.println(p.isOpen(2, 2));
// System.out.println(p.isOpen(2, 1));
// System.out.println(p.isOpen(2, 3));
// System.out.println(p.isOpen(1, 2));
// System.out.println(p.isOpen(2, 3));
// System.out.println(p.isOpen(3, 2));
//
// System.out.println("Open sites = " + p.numberOfOpenSites());
}
}