import edu.princeton.cs.algs4.WeightedQuickUnionUF;


public class Percolation {
    private int length;
    private int topVirtualNode;
    private int bottomVirtualNode;
    private int openSites;

    private WeightedQuickUnionUF uf;
    private boolean isOpenRec[];


    public Percolation(int n) {
        if (n <= 0) throw new IllegalArgumentException();

        uf = new WeightedQuickUnionUF((n * n) + 2);
        isOpenRec = new boolean[(n * n) + 2];
        length = n;
        topVirtualNode = n * n;
        bottomVirtualNode = (n * n) + 1;
        openSites = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i; j++) {
                isOpenRec[index(i + 1, j + 1)] = false;
            }
        }
    }

    public static void main(String[] args) {
    }

    private int index(int row, int col) {
        if (row <= 0 || row > length || col <= 0 || col > length)
            throw new IllegalArgumentException();

        return ((row - 1) * length) + col - 1;
    }

    public void open(int row, int col) {
        int site = index(row, col);
        if (!isOpenRec[site]) {
            isOpenRec[site] = true;
            openSites++;
        }

        try {
            int left = index(row, col - 1);
            if (isOpenRec[left] && !uf.connected(site, left)) uf.union(site, left);
        } catch (IllegalArgumentException e) {
        }

        try {
            int right = index(row, col + 1);
            if (isOpenRec[right] && !uf.connected(site, right)) uf.union(site, right);
        } catch (IllegalArgumentException e) {
        }

        try {
            int up = index(row - 1, col);
            if (isOpenRec[up] && !uf.connected(site, up)) uf.union(site, up);
        } catch (IllegalArgumentException e) {
            if (!uf.connected(site, topVirtualNode)) uf.union(site, topVirtualNode);
        }

        try {
            int down = index(row + 1, col);
            if (isOpenRec[down] && !uf.connected(site, down)) uf.union(site, down);
        } catch (IllegalArgumentException e) {
            if (!uf.connected(site, bottomVirtualNode)) uf.union(site, bottomVirtualNode);
        }
    }

    public boolean isOpen(int row, int col) {
        int site = index(row, col);
        return isOpenRec[site];
    }

    public boolean isFull(int row, int col) {
        int site = index(row, col);
        return uf.connected(topVirtualNode, site);
    }

    public int numberOfOpenSites() {
        return openSites;
    }

    public boolean percolates() {
        return uf.connected(topVirtualNode, bottomVirtualNode);
    }
}

@@ -74,16 +74,17 @@
 ation %7B%0A
+%0A
     priv
@@ -118,24 +118,17 @@
  int top
-VirtualN
+n
 ode;%0A   
@@ -146,24 +146,17 @@
 t bottom
-VirtualN
+n
 ode;%0A   
@@ -177,16 +177,20 @@
 penSites
+ = 0
 ;%0A%0A    p
@@ -247,19 +247,20 @@
 n isOpen
-Rec
+Site
 %5B%5D;%0A%0A%0A  
@@ -414,19 +414,20 @@
   isOpen
-Rec
+Site
  = new b
@@ -478,24 +478,17 @@
      top
-VirtualN
+n
 ode = n 
@@ -506,24 +506,17 @@
   bottom
-VirtualN
+n
 ode = (n
@@ -529,31 +529,8 @@
 + 1;
-%0A        openSites = 0;
 %0A%0A  
@@ -608,36 +608,19 @@
 j++)
- %7B%0A               
  isOpen
-Rec
+Site
 %5Bind
@@ -658,75 +658,9 @@
     
-    %7D%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B
+%7D
 %0A   
@@ -968,27 +968,28 @@
  if (!isOpen
-Rec
+Site
 %5Bsite%5D) %7B%0A  
@@ -1000,27 +1000,28 @@
       isOpen
-Rec
+Site
 %5Bsite%5D = tru
@@ -1139,19 +1139,20 @@
  (isOpen
-Rec
+Site
 %5Bleft%5D &
@@ -1341,19 +1341,20 @@
  (isOpen
-Rec
+Site
 %5Bright%5D 
@@ -1543,19 +1543,20 @@
  (isOpen
-Rec
+Site
 %5Bup%5D && 
@@ -1682,32 +1682,25 @@
 ed(site, top
-VirtualN
+n
 ode)) uf.uni
@@ -1711,24 +1711,17 @@
 ite, top
-VirtualN
+n
 ode);%0A  
@@ -1809,19 +1809,20 @@
  (isOpen
-Rec
+Site
 %5Bdown%5D &
@@ -1957,32 +1957,25 @@
 site, bottom
-VirtualN
+n
 ode)) uf.uni
@@ -1985,32 +1985,25 @@
 site, bottom
-VirtualN
+n
 ode);%0A      
@@ -2120,11 +2120,12 @@
 Open
-Rec
+Site
 %5Bsit
@@ -2244,32 +2244,25 @@
 onnected(top
-VirtualN
+n
 ode, site);%0A
@@ -2403,24 +2403,17 @@
 cted(top
-VirtualN
+n
 ode, bot
@@ -2419,16 +2419,9 @@
 ttom
-VirtualN
+n
 ode)

Fix some formatting of Percolation