Algorithms, Part I WEEK1 Percolation
来源:互联网 发布:淘宝店铺怎么删除宝贝 编辑:程序博客网 时间:2024/06/09 15:02
能力有限,大家没思路的可以参考下。
Percolation.java
import edu.princeton.cs.algs4.WeightedQuickUnionUF;public class Percolation { private boolean[] list; private final int n; private int ia; private final WeightedQuickUnionUF UF; private final WeightedQuickUnionUF UFTop; public Percolation(int n) { this.n = n; this.ia = 0; list = new boolean[n * n + 1]; UF = new WeightedQuickUnionUF(n * n + 2); UFTop = new WeightedQuickUnionUF(n * n + 1); } public boolean isOpen(int i, int j) { this.check(i, j); return list[(i - 1) * n + j]; } public int numberOfOpenSites() { return this.ia; } private void check(int i, int j) { if (i < 1 || i > n) throw new IllegalArgumentException("??"); if (j < 1 || j > n) throw new IllegalArgumentException("??"); } private void union(int i, int j, int i1, int j1) { this.UF.union((i - 1) * n + j, (i1 - 1) * n + j1); this.UFTop.union((i - 1) * n + j, (i1 - 1) * n + j1); } public void open(final int i, final int j) { this.check(i, j); if (!this.isOpen(i, j)) { this.ia = this.ia + 1; list[(i - 1) * n + j] = true; if (i == 1) { UF.union((i - 1) * n + j, 0); UFTop.union((i - 1) * n + j, 0); } if (i == n) UF.union((i - 1) * n + j, n * n + 1); if (i - 1 > 0 && this.isOpen(i - 1, j)) this.union(i, j, i-1, j); if (i + 1 <= n && this.isOpen(i + 1, j)) this.union(i, j, i + 1, j); if (j + 1 <= n && this.isOpen(i, j + 1)) this.union(i, j, i, j + 1); if (j - 1 > 0 && this.isOpen(i, j - 1)) this.union(i, j, i, j - 1); } } public boolean isFull(int i, int j) { check(i, j); return UFTop.connected((i - 1) * n + j, 0); } public boolean percolates() { return UF.connected(0, n * n + 1); } public static void main(String[] args) { Percolation perc = new Percolation(3); perc.open(1, 2); perc.open(2, 2); perc.open(2, 3); System.out.println(perc.UF.find(0)); for (int i = 1; i <= 3; i++) System.out.println(perc.UF.find((i-1)*3+1) + " " + perc.UF.find((i-1)*3+2) + " " + perc.UF.find((i-1)*3+3)); System.out.println(perc.UF.find(10)); System.out.println(perc.UFTop.find(0)); for (int i = 1; i <= 3; i++) System.out.println(perc.UFTop.find((i-1)*3+1) + " " + perc.UFTop.find((i-1)*3+2) + " " + perc.UFTop.find((i-1)*3+3)); System.out.println(perc.percolates()); System.out.println(perc.numberOfOpenSites()); }}
PercolationStats.java
public class PercolationStats { final private int N; final private double[] list; public PercolationStats(int N, int T) { if (N <= 0) throw new IllegalArgumentException(); if (T <= 0) throw new IllegalArgumentException(); this.N = N; this.list = new double[T]; for (int i = 0; i < T; i++) { Percolation per = new Percolation(N); double t1 = 0; while (!per.percolates()) { int i1 = StdRandom.uniform(N) + 1; int j1 = StdRandom.uniform(N) + 1; if (!per.isOpen(i1, j1)) { t1 = t1 + 1; per.open(i1, j1); } } list[i] = t1 / (N * N); } } public double mean() { return StdStats.mean(list); } public double stddev() { return StdStats.stddev(list); } public double confidenceLo() { return this.mean() - 1.96 * this.stddev() / Math.sqrt(N); } public double confidenceHi() { return this.mean() + 1.96 * this.stddev() / Math.sqrt(N); } public static void main(String[] args) { int N = StdIn.readInt(); int T = StdIn.readInt(); PercolationStats percolationStats = new PercolationStats(N, T); StdOut.println("mean = " + percolationStats.mean()); StdOut.println("stddev = " + percolationStats.stddev()); StdOut.println("95% confidence interval " + percolationStats.confidenceLo() + ", " + percolationStats.confidenceHi()); }}
阅读全文
0 0
- Algorithms, Part I WEEK1 Percolation
- Coursera.Algorithms.Part.I.Week.1.Percolation
- Algorithms, Part I
- Algorithms, Part I
- 【算法】Algorithms Part I week2
- Coursera Algorithms Percolation解题记录
- Algorithms,part1 week1
- Princeton Algorithms: Part 1 [Final Exam Part I]
- Coursera Algorithms Programming Assignment 1: Percolation
- Week1 Assignment - Princeton-Algorithms-PartI
- week1-10Theory of algorithms
- 普林斯顿大学算法第一部分学习总结(Week1-Percolation)
- 【Java、算法】Princeton Algorithms Part I Week 1 Exercise
- 【Coursera】Algorithms, Part I 算法C++实现: Quick Union
- 【Coursera】Algorithms, Part I 算法C++实现:Weighted Quick Union
- 【Coursera】Algorithms, Part I 算法C++实现:BinarySearch
- 【Coursera】Algorithms, Part I 算法C++实现:Stacks Linked-list
- [Stanford Cryptography I] Week1
- inline内联函数
- implicit declaration of function 'init_MUTEX'
- python编程规范
- POJ 1936.All in All
- Python Dict和Set(4)
- Algorithms, Part I WEEK1 Percolation
- Linux DRM(二)基本概念和特性
- LeetCode 第17题:Letter Combinations of a Phone Number
- 用TextView实现富文本展示,点击断句和语音播报
- jenkins入门手册----02第二章 Jenkins安装与配置
- Python动态爬虫
- Android 6.0 运行时权限管理(转载)
- ubuntu安装TI SDK无反应(响应)
- mac环境下配置gradle