如何在我的路径中显示从上到下的泄漏

问题描述

我使用了 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));
    }

}

Output

解决方法

似乎禁用双缓冲会得到你想要的结果:

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);
    }
}

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...