summaryrefslogtreecommitdiffstats
path: root/Algorithms
diff options
context:
space:
mode:
Diffstat (limited to 'Algorithms')
-rw-r--r--Algorithms/Part-I/1-Percolation/Percolation.java18
1 files 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));
}