我在使用加权快速联合的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

最佳答案

在我看来,您是从左到右而不是从上到下渗透(您将i用作列,j用作行)。

尝试更改printGrid,使用isOpen(y,x)代替isOpen(x,y)。

07-24 09:53