diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-03-05 00:16:12 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2013-11-15 17:38:13 +0100 |
commit | e372893b90da7b0e68741f5e74a984bdbcf6cc2f (patch) | |
tree | 0f5924b9f327759d70b756b491cb79eb5776f274 /Algorithms/Part-I | |
parent | 14b420d69d51ea37a3e89f1787ec22c9406dd39b (diff) | |
download | coursera-e372893b90da7b0e68741f5e74a984bdbcf6cc2f.zip coursera-e372893b90da7b0e68741f5e74a984bdbcf6cc2f.tar.gz |
Algorithms-I : 1-Percolation: use boolean instead of byte
Diffstat (limited to 'Algorithms/Part-I')
-rw-r--r-- | Algorithms/Part-I/1-Percolation/Percolation.java | 28 |
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? |