普林斯頓演算法-Percolation(滲透問題)
阿新 • • 發佈:2019-01-10
底層演算法都打包好了,可下載直接用,問題一下子就能應用
在判斷block時,用一個數組表示,頭尾各增加一個所謂的“隱藏節點”
->
Percolation.java
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Percolation {
private WeightedQuickUnionUF uf;
private int N;
private boolean[] sites;
private int count;
// create n-by-n grid, with all sites blocked
public Percolation(int n) {
this.N = n;
this.sites = new boolean[N*N+2];
sites[0] = true;
sites[N*N+1] = true;
count = 0;
uf = new WeightedQuickUnionUF(N*N+2 );
}
// open site (row, col) if it is not open already
public void open(int row, int col) {
if(row < 1 || row > N || col < 1 || col > N)
throw new IndexOutOfBoundsException("index out of bounds");
if(sites[(row-1)*N+col])
return;
sites[(row-1 )*N+col] = true;// open this site
count++;
// head & tail
if(row == 1)
uf.union(0,(row-1)*N+col);
if(row == N)
uf.union((row-1)*N+col,N*N+1);
// union judge for 4 directions
if(col != 1 && sites[(row-1)*N+col-1])
uf.union((row-1)*N+col,(row-1)*N+col-1);
if(col != N && sites[(row-1)*N+col+1])
uf.union((row-1)*N+col,(row-1)*N+col+1);
if(row != 1 && sites[(row-2)*N+col])
uf.union((row-1)*N+col,(row-2)*N+col);
if(row != N && sites[row*N+col])
uf.union((row-1)*N+col,row*N+col);
}
public boolean isOpen(int row, int col) {
if(row < 1 || row > N || col < 1 || col > N)
throw new IndexOutOfBoundsException("index out of bounds");
return sites[(row-1)*N+col];
}
//connected to an open site in the top row
public boolean isFull(int row, int col) {
if(row < 1 || row > N || col < 1 || col > N)
throw new IndexOutOfBoundsException("index out of bounds");
return uf.connected((row-1)*N+col,0);
}
// number of open sites
public int numberOfOpenSites() {
return count;
}
// does the system percolate?
public boolean percolates(){
return uf.connected(0,N*N+1);
}
public static void main(String[] args) {
try{
FileInputStream input = new FileInputStream("heart25.txt");
System.setIn(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int n = StdIn.readInt();
Percolation percolation = new Percolation(n);
while (!StdIn.isEmpty()) {
int row = StdIn.readInt();
int col = StdIn.readInt();
percolation.open(row, col);
}
System.out.println(percolation.percolates());
System.out.println(percolation.numberOfOpenSites());
}
}
PercolationStats.java
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
import java.util.Scanner;
public class PercolationStats {
private int sidelength;
private double mean;
private double stddev;
private double conLo;
private double conHi;
private double[] experiments;
// perform trials independent experiments on an n-by-n grid
public PercolationStats(int n, int trials) {
if(n <= 0 || trials <= 0)
throw new IllegalArgumentException("Illegal Argument");
sidelength = n;
if(sidelength == 1){
mean = 1;
stddev = Double.NaN;
conLo = Double.NaN;
conHi = Double.NaN;
}
else{
experiments = new double[trials];
for(int i=0; i<trials; i++){
Percolation checkPerco = new Percolation(n);
int count = 0;
while(!checkPerco.percolates()) {
int row = StdRandom.uniform(n) + 1;
int col = StdRandom.uniform(n) + 1;
if(!checkPerco.isOpen(row,col)){
checkPerco.open(row,col);
count++;
}
}
experiments[i] = (double)count/(n*n);
}
mean = StdStats.mean(experiments);
stddev = StdStats.stddev(experiments);
conLo = mean - (1.96 * stddev) / Math.sqrt(trials);
conHi = mean + (1.96 * stddev) / Math.sqrt(trials);
}
}
public double mean() {
return mean;
}
public double stddev() {
return stddev;
}
public double confidenceLo() {
return conLo;
}
public double confidenceHi() {
return conHi;
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trails = Integer.parseInt(args[1]);
PercolationStats per = new PercolationStats(n,trails);
System.out.println("mean: " + per.mean());
System.out.println("stddev: " + per.stddev());
System.out.println("confidence Low: "
+ per.confidenceLo());
System.out.println("confidence High: "
+ per.confidenceHi());
}
}
順帶一提這個課的Judge機制很棒