Programming Assignment 1: Percolation

Percolation API

public class Percolation {
   public Percolation(int n)                // create n-by-n grid, with all sites blocked
   public void open(int row, int col)       // open site (row, col) if it is not open already
   public boolean isOpen(int row, int col)  // is site (row, col) open?
   public boolean isFull(int row, int col)  // is site (row, col) full?
   public int numberOfOpenSites()           // number of open sites
   public boolean percolates()              // does the system percolate?

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

isFull()中,蓝色区域为full,即连通区域内的白色方块。

Programming Assignment 1: Percolation

Programming Assignment 1: Percolation

 

我们虚设了两个方块,来判断整个系统的连通性,为了解决回渗问题,建立2个检测WeightedUnion.当来到最后一层的时候,一个与n*n+1相连接,一个不连,最后判断isfull()时可以避免回渗问题。

PercolationStats API

 

public class PercolationStats {
  public PercolationStats(int n, int trials)    // perform trials independent experiments on an n-by-n grid
  public double mean()                          // sample mean of percolation threshold
  public double stddev()                        // sample standard deviation of percolation threshold
  public double confidenceLo()                  // low  endpoint of 95% confidence interval
  public double confidenceHi()                  // high endpoint of 95% confidence interval

  public static void main(String[] args)        // test client (described below)
}

 

 


蒙特卡洛算法,对于在n*n个方格图,随机打开一个方格,检测是否percolate。

上一篇:[PTA]练习2-1 Programming in C is fun!


下一篇:大学四年学习内容规划