summaryrefslogtreecommitdiffstats
path: root/Algorithms
diff options
context:
space:
mode:
Diffstat (limited to 'Algorithms')
-rw-r--r--Algorithms/Part-I/1-Percolation/Percolation.java28
1 files changed, 14 insertions, 14 deletions
diff --git a/Algorithms/Part-I/1-Percolation/Percolation.java b/Algorithms/Part-I/1-Percolation/Percolation.java
index efe79e8..1c2567e 100644
--- a/Algorithms/Part-I/1-Percolation/Percolation.java
+++ b/Algorithms/Part-I/1-Percolation/Percolation.java
@@ -3,7 +3,7 @@
public class Percolation
{
private int N;
- private byte[] opened;
+ private boolean[] opened;
private WeightedQuickUnionUF uf;
// create N-by-N grid, with all sites blocked
@@ -12,9 +12,9 @@ public class Percolation
this.N = N;
int n = (N*N)+2;
uf = new WeightedQuickUnionUF(n);
- opened = new byte[n];
- opened[0] = 1;
- opened[n-1] = 1;
+ opened = new boolean[n];
+ opened[0] = true;
+ opened[n-1] = true;
}
// open site (row i, column j) if it is not already
@@ -23,20 +23,20 @@ public class Percolation
if (i < 1 || j < 1 || i > N || j > N)
throw new java.lang.IndexOutOfBoundsException("check your indexes");
int x = (j-1)*N+i;
- if (opened[x] == 1) return;
- opened[x] = 1;
- if (i > 1 && opened[x-1] == 1) uf.union(x, x-1);
- if (i < N && opened[x+1] == 1) uf.union(x, x+1);
+ if (opened[x]) return;
+ opened[x] = true;
+ if (i > 1 && opened[x-1]) uf.union(x, x-1);
+ if (i < N && opened[x+1]) uf.union(x, x+1);
if (j == 1) {
uf.union(x, 0);
- if (opened[x+N] == 1) uf.union(x, x+N);
+ if (opened[x+N]) uf.union(x, x+N);
}
else if (j == N) {
uf.union(x, N*N+1);
- if (opened[x-N] == 1) uf.union(x, x-N);
+ if (opened[x-N]) uf.union(x, x-N);
} else {
- if (opened[x-N] == 1) uf.union(x, x-N);
- if (opened[x+N] == 1) uf.union(x, x+N);
+ if (opened[x-N]) uf.union(x, x-N);
+ if (opened[x+N]) uf.union(x, x+N);
}
}
@@ -45,7 +45,7 @@ public class Percolation
{
if (i < 1 || j < 1 || i > N || j > N)
throw new java.lang.IndexOutOfBoundsException("check your indexes");
- return (opened[(j-1)*N+i] == 1);
+ return opened[(j-1)*N+i];
}
// is site (row i, column j) full?
@@ -54,7 +54,7 @@ public class Percolation
if (i < 1 || j < 1 || i > N || j > N)
throw new java.lang.IndexOutOfBoundsException("check your indexes");
int x = (j-1)*N+i;
- return ((opened[x] == 1) && (uf.find(x) == 0));
+ return ((opened[x]) && (uf.find(x) == 0));
}
// does the system percolate?