From 7ceb4cb342b5d9b90b89edebe02ed8f03dd73115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 5 Mar 2013 13:41:02 +0100 Subject: Algorithms-I : 1-Percolation: I stupidly inverted (i,j) coordinates --- Algorithms/Part-I/1-Percolation/Percolation.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Algorithms/Part-I/1-Percolation/Percolation.java b/Algorithms/Part-I/1-Percolation/Percolation.java index 1c2567e..8364585 100644 --- a/Algorithms/Part-I/1-Percolation/Percolation.java +++ b/Algorithms/Part-I/1-Percolation/Percolation.java @@ -22,17 +22,17 @@ 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; + int x = (i-1)*N+j; 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 (j > 1 && opened[x-1]) uf.union(x, x-1); + if (j < N && opened[x+1]) uf.union(x, x+1); + if (i == 1) { + uf.union(0, x); if (opened[x+N]) uf.union(x, x+N); } - else if (j == N) { - uf.union(x, N*N+1); + else if (i == N) { + uf.union(N*N+1, x); if (opened[x-N]) uf.union(x, x-N); } else { 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]; + return opened[(i-1)*N+j]; } // is site (row i, column j) full? @@ -53,7 +53,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; + int x = (i-1)*N+j; return ((opened[x]) && (uf.find(x) == 0)); } -- cgit v1.1-2-g2b99