您的位置:首页 > 编程语言 > Go语言

princeton algorithms part 1 第一周作业题

2018-01-03 02:01 288 查看
import edu.princeton.cs.algs4.WeightedQuickUnionUF;

public class Percolation {
private WeightedQuickUnionUF uf;
private WeightedQuickUnionUF uf_backwash;
private int n;
private boolean[] ifopen;
private int count = 0;

public Percolation(int n)                // create n-by-n grid, with all sites blocked
{
if (n<=0) throw new IllegalArgumentException("n is<=0");
uf= new WeightedQuickUnionUF((n+1)*n+n+1);
uf_backwash = new WeightedQuickUnionUF(n*n+n+1);
this.n = n;
ifopen = new boolean[(n+ 1) * (n) + n + 1];
for(int i=0*n+1;i<=0*n+n;i++)
{
uf.union(0 * n + 1, i);
uf_backwash.union(0 * n + 1, i);
ifopen[i]=true;
uf.union((n+1)*n+1, (n+1)*n+i);
ifopen[(n+1)*n+i] = true;
}
}

public    void open(int row, int col)    // open site (row, col) if it is not open already
{
if (row<1||row>n) throw new IllegalArgumentException("row index out of bounds");
if (col<1||col>n)  throw new IllegalArgumentException("column index  out of bounds");

if (! this.isOpen(row, col) )
{
count++;
ifopen[row*n+col] = true;

if (ifopen[n*(row-1)+col])
{
uf.union(n * (row
4000
- 1) + col, n * row + col);
uf_backwash.union(n * (row - 1) + col, n * row + col);
}

if (ifopen[n*(row+1)+col])
{
uf.union(n * (row + 1) + col, n * row + col);
if(row != n)
uf_backwash.union(n * (row +1) + col, n * row + col);
}

if(col != 1 && ifopen[n*row+col-1] )
{
uf.union(n*row+col-1,n*row+col);
uf_backwash.union(n*row+col-1,n*row+col);
}

if(col != n && ifopen[n*row+col+1] )
{
uf.union(n*row+col+1,n*row+col);
uf_backwash.union(n*row+col+1,n*row+col);
}
}
}

public boolean isOpen(int row, int col)  // is site (row, col) open?
{
if (row<1||row>n) throw new IllegalArgumentException("row index out of bounds");
if(col<1||col>n)  throw new IllegalArgumentException("column index  out of bounds");
return ifopen[row*n+col];
}

public boolean isFull(int row, int col)  // is site (row, col) full?
{
if (row<1||row>n) throw new IllegalArgumentException("row index out of bounds");
if(col<1||col>n)  throw new IllegalArgumentException("column index  out of bounds");
return uf_backwash.connected(n*row+col,0*n+1);
}

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

public boolean percolates()              // does the system percolate?
{
return uf.connected(0*n+1,(n+1)*n+1);
}

public static void main(String[] args){
Percolation percolation = new Percolation(4);
System.out.println(percolation.percolates());
percolation.open(1, 1);
percolation.open(2, 1);
System.out.println(percolation.percolates());
percolation.open(3, 1);
percolation.open(3, 2);
System.out.println(percolation.isFull(3, 2));
percolation.open(4, 2);
System.out.println(percolation.percolates());
}
}


import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;

public class PercolationStats {
private double staT[];
private double sta_mean;
private double sta_stddev;
private int trials ;

public PercolationStats(int n, int trials)    // perform trials independent experiments on an n-by-n grid
{
if(n<=0) throw new IllegalArgumentException();
if(trials<=0) throw new IllegalArgumentException();
staT=new double[trials];
this.trials=trials;
int times=0;

while(times<trials)
{

Percolation pe = new Percolation(n);
while (!pe.percolates())
{
int x = StdRandom.uniform(n) + 1;
int y = StdRandom.uniform(n) + 1;
pe.open(x,y);
}
staT[times]=(double)pe.numberOfOpenSites()/((double)n*(double)n);
times++;
}
this.sta_mean = StdStats.mean(staT);
this.sta_stddev = StdStats.stddev(staT);
}
public double mean()                          // sample mean of percolation threshold
{
return this.sta_mean;
}
public double stddev()                        // sample standard deviation of percolation threshold
{
return this.sta_stddev;
}
public double confidenceLo()                  // low  endpoint of 95% confidence interval
{
return this.sta_mean-1.96*this.sta_stddev/Math.sqrt(trials);
}
public double confidenceHi()                  // high endpoint of 95% confidence interval
{
return this.sta_mean+1.96*this.sta_stddev/Math.sqrt(trials);
}

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

PercolationStats percolationStats = new PercolationStats(20, 100);
System.out.println("mean="+ percolationStats.mean());
System.out.println("stddev="+ percolationStats.stddev());
System.out.println("95%% confidence Interval="+percolationStats.confidenceLo()+"  "+ percolationStats.confidenceHi());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐