PercolationDirectedNonrecursive.java


Below is the syntax highlighted version of PercolationDirectedNonrecursive.java from §2.4 Case Study: Percolation.


/******************************************************************************
 *  Compilation:  javac PercolationDirectedNonrecursive.java
 *  Execution:    java PercolationDirectedNonrecursive < input.txt
 *  Dependencies: StdArrayIO.java StdOut.java
 *
 *  % java PercolationDirectedNonrecursive < testT.txt
 *  6 6
 *  0 1 1 0 0 0 
 *  0 0 1 1 1 1 
 *  0 0 0 1 1 0 
 *  0 0 0 0 1 1 
 *  0 1 1 1 1 1 
 *  1 1 0 1 0 0 
 *  true
 *
 *  % java PercolationDirectedNonrecursive < testF.txt
 *  6 6
 *  0 1 0 1 0 0 
 *  1 1 1 0 0 0 
 *  1 0 1 1 0 0 
 *  0 0 0 1 1 1 
 *  0 0 0 0 0 1 
 *  0 0 0 0 0 0 
 *  false
 *
 ******************************************************************************/

public class PercolationDirectedNonrecursive { 

    // given an n-by-n matrix of open sites, return an n-by-n matrix
    // of sites reachable from the top
    public static boolean[][] flow(boolean[][] isOpen) {
        int n = isOpen.length;
        boolean[][] isFull = new boolean[n][n];
        for (int j = 0; j < n; j++)
            isFull[0][j] = isOpen[0][j];

        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n; j++) {
                boolean filled = false;
                for (int k = j; (k < n) && isOpen[i][k]; k++)
                    filled = filled || isFull[i-1][k];
                for (         ; (j < n) && isOpen[i][j]; j++)
                    if (filled) isFull[i][j] = true;
            }
        }
        return isFull;
    }

    // does the system percolate down
    public static boolean percolates(boolean[][] isOpen) {
        boolean[][] isFull = flow(isOpen);
        int n = isFull.length;
        for (int j = 0; j < n; j++)
            if (isFull[n-1][j]) return true;
        return false;
    }

    // draw the n-by-n boolean matrix to standard draw
    public static void show(boolean[][] a, boolean which) {
        int n = a.length;
        StdDraw.setXscale(-1, n);
        StdDraw.setYscale(-1, n);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (a[i][j] == which)
                    StdDraw.filledSquare(j, n-i-1, 0.5);
    }

    // return a random n-by-n boolean matrix, where each entry is
    // true with probability p
    public static boolean[][] random(int n, double p) {
        boolean[][] a = new boolean[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = StdRandom.bernoulli(p);
        return a;
    }

    // test client
    public static void main(String[] args) {
        boolean[][] isOpen = StdArrayIO.readBoolean2D();
        StdArrayIO.print(flow(isOpen));
        StdOut.println(percolates(isOpen));
    }
} 


Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Aug 30 09:58:33 EDT 2016.