I used the percolation class from the algs4.jar library, where you can simulate a whole labyrinth and see where the leaks are. I want to show the leaks that are from top to bottom, but now I get to see all of the leaks.
Does anyone know how I can see only the leaks that are percolations?
I thought maybe using dfs from the flow method in de Percolation.java class, and say something like only show when index i in rows is maxlength and marked but I don't really know how to say that, because I am not sure if this statement will show the entire leak of just the max length leak.
code I run AssignmentTwo.java:
import edu.princeton.cs.algs4.StdDraw;
public class AssignmentTwo {
public static void main(String[] args) {
int n = 500;
double p = 0.60;
int trials = 20;
// repeatedly created n-by-n matrices and display them using standard draw
StdDraw.enableDoubleBuffering();
for (int t = 0; t < trials; t++) {
boolean[][] open = Percolation.random(n, p);
StdDraw.clear();
StdDraw.setPenColor(StdDraw.PINK);
Percolation.show(open, false);
StdDraw.setPenColor(StdDraw.BLACK);
boolean[][] full = Percolation.flow(open);
Percolation.show(full, true);
StdDraw.show();
StdDraw.pause(1000);
}
}
}
and this is the Percolation.java class:
import edu.princeton.cs.algs4.StdArrayIO;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
public class Percolation {
// 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++) {
flow(isOpen, isFull, 0, j);
}
return isFull;
}
// determine set of full sites using depth first search
public static void flow(boolean[][] isOpen, boolean[][] isFull, int i, int j) {
int n = isOpen.length;
// base cases
if (i < 0 || i >= n) return; // invalid row
if (j < 0 || j >= n) return; // invalid column
if (!isOpen[i][j]) return; // not an open site
if (isFull[i][j]) return; // already marked as full
// mark i-j as full
isFull[i][j] = true;
flow(isOpen, isFull, i+1, j); // down
flow(isOpen, isFull, i, j+1); // right
flow(isOpen, isFull, i, j-1); // left
flow(isOpen, isFull, i-1, j); // up
}
// does the system percolate?
public static boolean percolates(boolean[][] isOpen) {
int n = isOpen.length;
boolean[][] isFull = flow(isOpen);
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));
}
}
It seems that disabling double buffering will get you the result you want:
public class AssignmentTwo {
public static void main(String[] args) {
int n = 500;
double p = 0.60;
StdDraw.enableDoubleBuffering();
boolean[][] open = Percolation.random(n, p);
StdDraw.clear();
StdDraw.setPenColor(StdDraw.PINK);
Percolation.show(open, false);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.show();
StdDraw.disableDoubleBuffering();
boolean[][] full = Percolation.flow(open);
Percolation.show(full, true);
}
}