[Algorithms(Princeton)] Week1 - Percolation
public class Percolation {
private boolean[] openSites;
private int gridN;
private WeightedQuickUnionUF UF;
private WeightedQuickUnionUF UFfull; public Percolation(int N) {
if (N <= 0) {
throw new java.lang.IllegalArgumentException(
"N must be greater than 0");
}
openSites = new boolean[N * N];
gridN = N;
for (int i = 0; i < N * N; ++i) {
openSites[i] = false;
}
// add 2 virtual sites
UF = new WeightedQuickUnionUF(N * N + 2);
UFfull = new WeightedQuickUnionUF(N * N + 1);
} private void indexChecker(int i, int j) {
if (i < 1 || j < 1 || i > gridN || j > gridN)
throw new java.lang.IndexOutOfBoundsException();
} public void open(int i, int j) {
indexChecker(i, j); int indexI = i - 1;
int indexJ = j - 1; int osIndex = indexI * gridN + indexJ;
if (openSites[osIndex])
return;
openSites[osIndex] = true; int ufIndex = indexI * gridN + indexJ + 1;
if (indexI == 0) {
UF.union(0, ufIndex);
UFfull.union(0, ufIndex);
}
if (indexI == gridN - 1) {
UF.union(ufIndex, gridN * gridN + 1);
} boolean bOpen = false; // union adjacent open sites
int leftIndexI = indexI;
int leftIndexJ = indexJ - 1;
if (leftIndexJ >= 0) {
bOpen = isOpen(leftIndexI + 1, leftIndexJ + 1);
if (bOpen) {
int leftUFIndex = leftIndexI * gridN + leftIndexJ + 1;
UF.union(leftUFIndex, ufIndex);
UFfull.union(leftUFIndex, ufIndex);
}
} int rightIndexI = indexI;
int rightIndexJ = indexJ + 1;
if (rightIndexJ < gridN) {
bOpen = isOpen(rightIndexI + 1, rightIndexJ + 1);
if (bOpen) {
int rightUFIndex = rightIndexI * gridN + rightIndexJ + 1;
UF.union(ufIndex, rightUFIndex);
UFfull.union(ufIndex, rightUFIndex);
}
} int upIndexI = indexI - 1;
int upIndexJ = indexJ;
if (upIndexI >= 0) {
bOpen = isOpen(upIndexI + 1, upIndexJ + 1);
if (bOpen) {
int upUFIndex = upIndexI * gridN + upIndexJ + 1;
UF.union(upUFIndex, ufIndex);
UFfull.union(upUFIndex, ufIndex);
}
} int downIndexI = indexI + 1;
int downIndexJ = indexJ;
if (downIndexI < gridN) {
bOpen = isOpen(downIndexI + 1, downIndexJ + 1);
if (bOpen) {
int downUFIndex = downIndexI * gridN + downIndexJ + 1;
UF.union(ufIndex, downUFIndex);
UFfull.union(ufIndex, downUFIndex);
}
}
} public boolean isOpen(int i, int j) {
indexChecker(i, j);
return (openSites[(i - 1) * gridN + j - 1]);
} public boolean isFull(int i, int j) {
indexChecker(i, j);
int indexI = i - 1;
int indexJ = j - 1; int osIndex = indexI * gridN + indexJ;
int ufIndex = osIndex + 1; boolean bOpen = isOpen(i, j);
boolean isFull = UFfull.connected(0, ufIndex);
return (bOpen && isFull);
} public boolean percolates() {
if (gridN == 1)
return (openSites[0]);
return UF.connected(0, gridN * gridN + 1);
}
}
You can see Percolation problem here.
http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
This problem is something related to Union-Find.
[Algorithms(Princeton)] Week1 - Percolation的更多相关文章
- [Algorithms(Princeton)] Week1 - PercolationStats
public class PercolationStats { private int N; private int T; private double[] results; public Perco ...
- Coursera Algorithms Programming Assignment 1: Percolation(100分)
题目来源http://coursera.cs.princeton.edu/algs4/assignments/percolation.html 作业分为两部分:建立模型和仿真实验. 最关键的部分就是建 ...
- Coursera Algorithms week1 查并集 练习测验:3 Successor with delete
题目原文: Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form: Rem ...
- Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element
题目原文: Add a method find() to the union-find data type so that find(i) returns the largest element in ...
- Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity
题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...
- Princeton Algorithms week3 Assignment
这周编程作业是实现检测点共线的算法.和排序算法有关系的地方在于,对斜率排序后可以很快的检测出来哪些点是共线的,另外这个算法的瓶颈也在于排序的性能. 一点收获: java传参数时传递的是值,这很多人都知 ...
- Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...
- Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time
题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...
- Algorithms, Part I by Kevin Wayne, Robert Sedgewick
Welcome to Algorithms, Part I 前言 昨天在突然看到了Coursera上Robert Sedgewick讲的Algorithms,Part II看了一些,甚是爽快,所以又去 ...
随机推荐
- ZeroMQ(java)之I/O线程的实现与组件间的通信
算是开始读ZeroMQ(java)的代码实现了吧,现在有了一个大体的了解,看起来实现是比较的干净的,抽象什么的不算复杂... 这里先来看看它的I/O线程的实现吧,顺带看看是如何实现组件的通信的.... ...
- Java--读写文件综合
package javatest; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream ...
- Linux: xclip,pbcopy,xsel用法 terminal 复制粘帖 (mac , ubuntu)
ubuntu下的用户可以只用apt-get来安装: sudo apt-get install xclip echo "Hello, world" | xclip mac下使用pbc ...
- Timer1控件的属性
- jsp回车键登录代码
$("body").keydown(function(event) { if (event.keyCode == "13") {// keyCode=13是回车 ...
- mybatis的insert简单使用
- 【python】choice函数
来源:http://www.runoob.com/python/func-number-choice.html 描述 choice() 方法返回一个列表,元组或字符串的随机项. 语法 以下是 choi ...
- Fresco 源码分析(三) Fresco服务端处理(1) ImagePipeline为何物
4.3 服务端的处理 备注: 因为是分析,而不是设计,所以很多知识我们类似于插叙的方式叙述,就是用到了哪个知识点,我们再提及相关的知识点,如果分析到了最后,我想想是不是应该将这个架构按照设计的方式,重 ...
- Virtual Box创建共享目录
1.先关闭ubuntu,在virtualbox“设置”中找到“共享文件夹”,点击进入,点击右边添加目录按钮,添加windows中要共享的目录,取一个名.比如我在D盘建一个名为share的文件夹,如下图 ...
- 网易前端JavaScript编码规范
在多年开发邮箱webmail过程中,网易邮箱前端团队积累了不少心得体会,我们开发了很多基础js库,实现了大量前端效果组件,开发了成熟的opoa框架以及api组件,在此向大家做一些分享.今天想先和大家聊 ...