[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看了一些,甚是爽快,所以又去 ...
随机推荐
- String是引用类型
关于String为值类型还是引用类型的讨论一直没有平息,最近一直在研究性能方面的问题,今天再次将此问题进行一次明确.希望能给大家带来点帮助. 如果有错误请指出. 来看下面例子: //值类型 ; int ...
- ORM框架是什么
ORM框架是什么 对象关系映射,目前数据库是关系型数据库 ORM 主要是把数据库中的关系数据映射称为程序中的对象 目前集中常见的ORM框架1 Nhibernate原因:用的比较多,资料也比较好找. ...
- SharePoint 2010 隐藏快速启动栏之使用内容编辑器webpart
SharePoint 2010 自带的webpart里有一个叫内容编辑,在媒体和内容分类里面: 将其添加到页面后效果: 点击用于添加新内容,此时注意Ribbon菜单中的变化: 这里可以看到,你可以插入 ...
- iOS 端的第三方语音识别库
最近在看语音识别方面的库,主要看了2个收费的项目,一个是 At&t 的,一个是Nuance的.这2个项目虽然是收费的,但是仅仅测试的话,是免费的,连接如下 https://developer. ...
- css3学习总结8--CSS3 3D转换
3D 转换 1. rotateX() 2. rotateY() otateX() 方法 通过 rotateX() 方法,元素围绕其 X 轴以给定的度数进行旋转. 示例: div { transform ...
- JPush Wiki
极光推送包含有通知与自定义消息两种类型的推送.本文描述他们的区别,以及建议的应用场景. 功能角度 通知 或者说 Push Notification,即指在手机的通知栏(状态栏)上会显示的一条通知信息. ...
- /bin/dd if=/path/to/source-file of=/path/to/backup-file
[root@ok virhost]# qemu-img info 05t.img image: 05t.img file format: raw virtual size: 10G (10737418 ...
- 4.抽象工厂模式(Abstract Factory)
using System; using System.Reflection; namespace ConsoleApplication1 { class Program { static void M ...
- IoC容器Autofac - Autofac + Asp.net MVC + EF Code First(转载)
转载地址:http://www.cnblogs.com/JustRun1983/archive/2013/03/28/2981645.html 有修改 Autofac通过Controller默认构造 ...
- ytu 2011: C语言实验——找中间数(水题)
2011: C语言实验——找中间数 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 212 Solved: 122[Submit][Status][Web ...