如何解决如何在我的路径中显示从上到下的泄漏
我使用了 algs4.jar 库中的 percolation 类,您可以在其中模拟整个迷宫并查看泄漏的位置。我想显示从上到下的泄漏,但现在我可以看到所有泄漏。
有谁知道我怎么只能看到渗漏的泄漏?
我想可能会在 de Percolation.java 类中使用 flow 方法中的 dfs,并说一些类似的内容,例如仅在行中的索引 i 为 maxlength 并标记时才显示,但我真的不知道如何这么说,因为我不确定这个语句是否会显示最大长度泄漏的整个泄漏。
我运行的代码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 类:
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,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,i+1,j); // down
flow(isOpen,i,j+1); // right
flow(isOpen,j-1); // left
flow(isOpen,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);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。