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的更多相关文章

  1. [Algorithms(Princeton)] Week1 - PercolationStats

    public class PercolationStats { private int N; private int T; private double[] results; public Perco ...

  2. Coursera Algorithms Programming Assignment 1: Percolation(100分)

    题目来源http://coursera.cs.princeton.edu/algs4/assignments/percolation.html 作业分为两部分:建立模型和仿真实验. 最关键的部分就是建 ...

  3. 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 ...

  4. 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 ...

  5. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity

    题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...

  6. Princeton Algorithms week3 Assignment

    这周编程作业是实现检测点共线的算法.和排序算法有关系的地方在于,对斜率排序后可以很快的检测出来哪些点是共线的,另外这个算法的瓶颈也在于排序的性能. 一点收获: java传参数时传递的是值,这很多人都知 ...

  7. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  8. 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 ...

  9. Algorithms, Part I by Kevin Wayne, Robert Sedgewick

    Welcome to Algorithms, Part I 前言 昨天在突然看到了Coursera上Robert Sedgewick讲的Algorithms,Part II看了一些,甚是爽快,所以又去 ...

随机推荐

  1. tomcat配置文件之Server.xml

    Server.xml包含的元素有<Server>.<Service>.<Connector>.<Engine>.<Host>.<Con ...

  2. HDU 1864最大报销额 01背包问题

    B - 最大报销额 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  3. Linux 怎么重命名多个文件

    下面是重命名命令的基本语法. rename [-v -n -f] <pcre> <files> <pcre> 是Perl兼容正则表达式,它表示的是要重命名的文件和该 ...

  4. HDOJ 2088

    #include<cstdio> int main() { ],i,j,n,x,k=; int sum,ans; while(scanf("%d",&n)!=E ...

  5. Xenomai 安装准备工作

    一些安装xenomai的参考资料: http://my.oschina.net/hevakelcj/blog/124290 http://blog.sina.com.cn/s/blog_60b9ee1 ...

  6. 【转】cas注册后自动登录

    本文转自:http://denger.iteye.com/blog/805743  1. 关于CAS的介绍不再累述,我想涉及过SSO同学应该都会对该框架所有了解,我们目前项目采用的CAS Server ...

  7. iOS __block类型变量作用域

    看下图 在c语言中,2个独立的函数是不可能互相访问局部变量的,但是__block提供了这个功能,它不单单能读变量,还可以对变量进行写!上图说明,block获得了i最后的真实值5,没有只取得0,这都是& ...

  8. 解决虚拟机 正在决定eht0 的ip信息失败 无链接-- 虚拟机上linux redhat 上网问题

    对于虚拟机上,linux redhat上网的配置方式有三种 一.用setup命令进行配置(具体技巧可查setup命令的使用) 二.直接用 ifconfig eth0  ip地址进行配置 三.进入系统文 ...

  9. Linux系统排查2——CPU负载篇

    本随笔介绍CPU负载的排查手段. 查看系统负载的工具:uptime,w,都能查看系统负载,系统平均负载是处于运行或不可打扰状态的进程的平均数, 可运行:运行态,占用CPU,或就绪态,等待CPU调度. ...

  10. Java for LeetCode 058 Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...