如何在我的路径中显示从上到下的泄漏
How to show top to bottom leaks in my path
我使用了 algs4.jar 库中的渗滤 class,您可以在其中模拟整个迷宫并查看泄漏点。我想显示从上到下的泄漏,但现在我看到了所有泄漏。
有谁知道我怎么只能看到渗透的泄漏?
我想也许可以在 de Percolation.java class 中使用 flow 方法中的 dfs,并说一些类似仅在行中的索引 i 为最大长度时显示的内容并标记但我真的不知道怎么说,因为我不确定这个声明是否会显示最大长度泄漏的整个泄漏。
代码我运行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);
}
}
}
这是 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));
}
}
看来禁用双缓冲会得到你想要的结果:
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);
}
}
我使用了 algs4.jar 库中的渗滤 class,您可以在其中模拟整个迷宫并查看泄漏点。我想显示从上到下的泄漏,但现在我看到了所有泄漏。
有谁知道我怎么只能看到渗透的泄漏?
我想也许可以在 de Percolation.java class 中使用 flow 方法中的 dfs,并说一些类似仅在行中的索引 i 为最大长度时显示的内容并标记但我真的不知道怎么说,因为我不确定这个声明是否会显示最大长度泄漏的整个泄漏。
代码我运行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);
}
}
}
这是 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));
}
}
看来禁用双缓冲会得到你想要的结果:
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);
}
}