[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看了一些,甚是爽快,所以又去 ...
随机推荐
- django 1.7 新特性 --- data migration
官方文档:https://docs.djangoproject.com/en/dev/topics/migrations/ 1.7 之前大家可能会用south用于管理数据库的模型的同步.1.7之后dj ...
- 又是一个二模02,不过day2
话说比较简单.除了第三题不会写平衡树啊你妹!!边做边写吧. 机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链 ...
- QQ,MSN,Skype在线客服代码
QQ,MSN,Skype在线客服代码 在网站建设时,为了更好的实施网站的营销型,会用到QQ,MSN等在线交流,以便客户能够快捷方便的联系我们.在这里,提供QQ,MSN的在线客服代码给大家分享: 1.Q ...
- excel中如何批量将所有的网址设为超链接
首先如果数据较少的话,只需要双击鼠标左键,回车,就会自动转换成超链接. 转自: http://zhidao.baidu.com/question/200363361.html?qbl=relate_q ...
- javascript动态添加一组input
2013年12月18日 20:56:29 场景: 批量添加 友情链接 功能 每个友情链接记录有3个字段:名字(name),超链接(url),排序(order) 要求每次点击"添加" ...
- 【python】choice函数
来源:http://www.runoob.com/python/func-number-choice.html 描述 choice() 方法返回一个列表,元组或字符串的随机项. 语法 以下是 choi ...
- Spring MVC前台使用html页面作为视图,配置静态资源后Controller控制器不起作用的解决办法
1.Spring MVC搭建项目的时候,想使用html页面作为前端的视图,你会发现html页面不能访问,原因是由于Spring拦截器将其拦截寻找控制器的缘故,解决办法就是配置静态资源: <mvc ...
- July 28th, Week 31st Thursday, 2016
Time is a bird flying into eternity. 时间是一只永远在飞翔的鸟儿. Time waits for nobody. Vitality shows in not onl ...
- ubuntu 14.04安装mysql server & mysql client
$ sudo apt-get install mysql-server
- POSIX线程--同时执行
//#define _REENTRANT//#define _POSIX_C_SOURCE#include <iostream>#include <string>#includ ...