Java中的渗流

Java中的渗流,java,Java,我在java中使用加权快速联合的过滤程序时遇到了问题。我认为主要是因为我使用联合来判断连接了哪些位置,或者我正在运行的测试有问题,以查看网格是否过滤。如果有人有反馈,我将不胜感激 /* a class to test for percolation of an N by N grid*/ public class Percolation{ WeightedQuickUnionUF uf; boolean[][] grid; public int size; int opensit

我在java中使用加权快速联合的过滤程序时遇到了问题。我认为主要是因为我使用联合来判断连接了哪些位置,或者我正在运行的测试有问题,以查看网格是否过滤。如果有人有反馈,我将不胜感激

/* a class to test for percolation of an N by N grid*/
public class Percolation{
  WeightedQuickUnionUF uf;
  boolean[][] grid;
  public int size;
  int opensites;//keeps track of open sites


 /*Constructor for Percolation that takes N as an argument 
  * to create an N*N grid
  **/
  public Percolation(int N){
    this.size=N;
    int spaces= N*N;
    uf = new WeightedQuickUnionUF(spaces+1);//initializes the WQUUF data structure.
    opensites=0;//intializes open sites to 0.
    this.grid = new boolean[N][N];// makes a boolean array 2D N by N representing the grid 
    for(int i=0;i<N;i++){//i represents the x coordinate
      for(int j=0;j<N;j++){// j represents the y coordinate
        grid[i][j]=false; //sets all spaces as closed
      }//end j loop
    }//end i loop

    for(int i=0;i<size;i++){
      uf.union(size*size,i);
    }// end for that connects all top sites
  }// end constructor;



/*
 * takes to ints as X and Y coordinates 
 * and opens that square on the grid
 **/
  public void open(int i, int j){

    if(!(this.grid[i][j])){
      this.grid[i][j]=true;
      opensites+=1;
    }
  }//end open


/*
 * takes to ints as X and Y coordinates and 
 * returns true if that space is open and false
 * if not
 **/
  public boolean isOpen(int i, int j){
    return grid[i][j];
  }//end isOpen




  /* 
   * takes to ints as X and Y coordinates and 
   * returns true if that space is full and false
   * if not
   */
  public boolean isFull(int i, int j){
    if(isOpen(i,j)){
      if(uf.connected((i+j*size),(size*size))){ return true;}
    }//end for
    return false;
  }//end isFull

  /* 
   * checks if any space on the bottom is full returns true 
   * if there is one false other wise
   */
  public boolean percolates(){
    for(int i=0;i<size;i++){
      if(isFull(i,size-1)) return true;
    }
    return false;
  }//end percolates

  /*
   * prints out grid 
   * ---------------------
   * 0 false true false
   * 1 true true true
   * 2 false true true
   * ---------------------
   */
  public void printGrid(){
    StdOut.println("----------------------------------");
    for(int x = 0;x < size;x++){
      for(int y = 0; y < size; y++){
        String mark=isOpen(x,y) ? "x": "0";
        StdOut.print("|"+mark+"|");

      }
      StdOut.println();
    }
    StdOut.println("----------------------------------");
  }


/*
 * Main method takes two int arguments one for the number 
 * of tests to be run and one for the size of the grid
 **/
  public static void main(String[] args) {
    int tests=StdIn.readInt();
    int size=StdIn.readInt();
    int testedTimes=1;
    int spaces=size*size-1;
    int check=0;
    int space=0;
    while(testedTimes<=tests){
      Percolation perc = new Percolation(size);
      StdOut.println("Before anything");
      perc.printGrid();
      while(!perc.percolates()){
        int x=StdRandom.uniform(0,size);
        int y=StdRandom.uniform(0,size);

        perc.open(x,y);
        space=x+(y*size);
        if((space+1<spaces)&&(x<size-1)){//right 1
          if(perc.isOpen(x+1,y)){
            perc.uf.union(space,space+1);
          }
        }
        if((space-1>0)&&(x>0)){//left 1
          if(perc.isOpen(x-1,y)){
            perc.uf.union(space,space-1);
          }
        }
        if((space-size>0)&&(y-1>0)){//up 1
          if(perc.isOpen(x,y-1)){
            perc.uf.union(space,space-size);
          }
        }
        if((space+size<spaces)&&(y+1<=size)){//down 1
          if((perc.isOpen(x,y+1))){
            perc.uf.union(space,space+size);
          }
          perc.printGrid();
        }

      }//end while(!percolates())
      testedTimes++;
    }//end while(TestedTimes<tests)
  }//end main.
}//end class
/*用于测试N×N网格渗流的类*/
公共类渗流{
加权快速uf;
布尔[][]网格;
公共整数大小;
int opensites;//跟踪打开的站点
/*以N为参数的渗流构造函数
*创建N*N网格的步骤
**/
公共渗滤(int N){
这个。大小=N;
int空间=N*N;
uf=new-WeightedQuickUnionUF(空格+1);//初始化WQUUF数据结构。
opensites=0;//将打开的站点初始化为0。
this.grid=new boolean[N][N];//生成一个表示网格的二维N乘N的布尔数组

对于(inti=0;i,我觉得你是从左到右渗透,而不是从上到下渗透(你使用i作为列,j作为行)

尝试更改打印网格,使用等参线(y,x)而不是等参线(x,y)。

导入edu.princeton.cs.algs4.WeightedQuickUnionUF;
公共类渗流{
私有布尔[][]网格;
私家侦探;
私有int top=0;
私有int底部;
私人加权快速联合uf;
私有整数计数=0;
//创建n×n网格,所有站点最初都被阻止
公共渗滤(int n){
if(n len | col>len){
抛出新的IllegalArgumentException();
}
int i=第1行;
int j=col-1;
如果(!等参线(行,列)){
计数++;
网格[i][j]=真;
如果(行==1){
union(getIndex(行、列)、top);
}
如果(行==len){
union(getIndex(行,列),底部);
}
//联合邻居
//向下
if(行<长度和等间距(行+1,列)){
union(getIndex(行,列),getIndex(行+1,列));
}
//向上
如果(行>1和等参线(行-1,列)){
union(getIndex(row,col),getIndex(row-1,col));
}
//左
如果(列>1和等参线(行,列-1)){
union(getIndex(row,col),getIndex(row,col-1));
}
//对
如果(列列| |列>列){
抛出新的IllegalArgumentException();
}
返回网格[行-1][列-1];
}
//站点(行、列)已满吗?
公共布尔值已满(整数行,整数列){
如果(行<1 | |列<1 | |列>列| |列>列){
抛出新的IllegalArgumentException();
}
返回(uf.find(getIndex(行,列))==uf.find(顶));
}
//返回打开的站点数
public int numberOfOpenSites(){
返回计数;
}
//系统是否渗透?
公共布尔渗流(){
返回(uf.find(顶部)==uf.find(底部));
}
//获取行、列的索引
私有int getIndex(int行,int列){
返回len*(行-1)+(列);
}
//测试客户端(可选)
公共静态void main(字符串[]args){
}
}

我想这是CodeReview的问题。谢谢,我刚刚在那里发布了它。你的实际问题是什么?有时它说它正在渗透,而其他时候它说它没有渗透,当它在CodeReview上发布时,他们说他们只清理代码,不帮助解决问题,只是为了清理代码实践
import edu.princeton.cs.algs4.WeightedQuickUnionUF;

public class Percolation {
private boolean[][] grid;
private int len;
private int top = 0;
private int bottom;
private WeightedQuickUnionUF uf;
private int count = 0;

// creates n-by-n grid, with all sites initially blocked
public Percolation(int n) {
    if (n <= 0) {
        throw new IllegalArgumentException();
    }
    len = n;
    grid = new boolean[n][n];
    bottom = n * n + 1;
    uf = new WeightedQuickUnionUF(n * n + 2);
}

// opens the site (row, col) if it is not open already
public void open(int row, int col) {
    if (row < 1 || col < 1 || row > len || col > len) {
        throw new IllegalArgumentException();
    }
    int i = row - 1;
    int j = col - 1;
    if (!isOpen(row, col)) {
        count++;
        grid[i][j] = true;
        if (row == 1) {
            uf.union(getIndex(row, col), top);
        }
        if (row == len) {
            uf.union(getIndex(row, col), bottom);
        }

        // union the neighbors
        // down
        if (row < len && isOpen(row + 1, col)) {
            uf.union(getIndex(row, col), getIndex(row + 1, col));
        }
        // up
        if (row > 1 && isOpen(row - 1, col)) {
            uf.union(getIndex(row, col), getIndex(row - 1, col));
        }
        // left
        if (col > 1 && isOpen(row, col - 1)) {
            uf.union(getIndex(row, col), getIndex(row, col - 1));
        }
        // right
        if (col < len && isOpen(row, col + 1)) {
            uf.union(getIndex(row, col), getIndex(row, col + 1));
        }
    }
}

// is the site (row, col) open?
public boolean isOpen(int row, int col) {
    if (row < 1 || col < 1 || row > len || col > len) {
        throw new IllegalArgumentException();
    }
    return grid[row - 1][col - 1];
}

// is the site (row, col) full?
public boolean isFull(int row, int col) {
    if (row < 1 || col < 1 || row > len || col > len) {
        throw new IllegalArgumentException();
    }
    return (uf.find(getIndex(row, col)) == uf.find(top));
}

// returns the number of open sites
public int numberOfOpenSites() {
    return count;
}

// does the system percolate?
public boolean percolates() {
    return (uf.find(top) == uf.find(bottom));
}

// get the index if row,column
private int getIndex(int row, int col) {
    return len * (row - 1) + (col);
}

// test client (optional)
public static void main(String[] args) {

}
}