From e372893b90da7b0e68741f5e74a984bdbcf6cc2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 5 Mar 2013 00:16:12 +0100 Subject: Algorithms-I : 1-Percolation: use boolean instead of byte --- Algorithms/Part-I/1-Percolation/Percolation.java | 28 ++++++++++++------------ 1 file 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? -- cgit v1.1-2-g2b99