Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f81cd79

Browse files
merging
1 parent 7b2720c commit f81cd79

File tree

7 files changed

+161
-112
lines changed

7 files changed

+161
-112
lines changed

‎UnionFind/.idea/workspace.xml‎

Lines changed: 143 additions & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 Bytes
Binary file not shown.
175 Bytes
Binary file not shown.
4 Bytes
Binary file not shown.

‎UnionFind/src/InteractivePercolationVisualizer.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ public static void main(String[] args) {
3030
StdDraw.show();
3131

3232
while (true) {
33-
3433
// detected mouse click
3534
if (StdDraw.mousePressed()) {
36-
3735
// screen coordinates
3836
double x = StdDraw.mouseX();
3937
double y = StdDraw.mouseY();

‎UnionFind/src/Percolation.java‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public class Percolation {
1313
* */
1414

1515
public Percolation(int n) {
16-
qSites = n;
17-
int limit = (n+2)*(n+2);
18-
sites = new boolean[limit+1];
19-
ds = new int[limit+1];
20-
sz = new int[limit+1];
16+
qSites = n;
17+
int limit = (n+2)*(n+2);
18+
sites = new boolean[limit+1];
19+
ds = new int[limit+1];
20+
sz = new int[limit+1];
2121
for(int i = 0; i<limit; i++) {
2222
ds[i] = i;
2323
sz[i] = 1;
@@ -60,8 +60,16 @@ public int numberOfOpenSites() {
6060
return openSites;
6161
}
6262

63+
private int root(int p) {
64+
if(p == ds[p])
65+
return p;
66+
return root(ds[p]);
67+
}
68+
6369
public boolean percolates() {
64-
return false;
70+
71+
72+
return root(ds[0]) == root(ds[qSites]);
6573
}
6674

6775
public static void main(String[] args) { }

‎UnionFind/src/PercolationVisualizer.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ else if (perc.isOpen(row, col)) {
5252
StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
5353
StdDraw.setPenColor(StdDraw.BLACK);
5454
StdDraw.text(0.25*n, -0.025*n, opened + " open sites");
55-
if (perc.percolates()) StdDraw.text(0.75*n, -0.025*n, "percolates");
56-
else StdDraw.text(0.75*n, -0.025*n, "does not percolate");
55+
if (perc.percolates())
56+
StdDraw.text(0.75*n, -0.025*n, "percolates");
57+
else
58+
StdDraw.text(0.75*n, -0.025*n, "does not percolate");
5759
}
5860

5961
public static void main(String[] args) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /