[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看了一些,甚是爽快,所以又去 ...
随机推荐
- cURL的几个经典实例
1.cURL请求的基本步骤: (1)初始化 (2)设置选项,包括URL (3)执行并获取HTML文档内容 (4)释放cURL句柄 <?php //1.初始化 $ch = curl_init(); ...
- Ninject学习笔记<一>
本文转载自永远的阿哲 如果给您带来不便请联系博主. Ninject是一款.Net平台下的开源依赖注入框架.按照官方说法,它快如闪电.超级轻量,且充分利用了.Net的最新语法,使用Lambda表达式代替 ...
- 16.O(logn)求Fibonacci数列[Fibonacci]
[题目] log(n)时间Fib(n),本质log(n)求a^n. [代码] C++ Code 12345678910111213141516171819202122232425262728293 ...
- 【转】Kettle集群
本文转自:http://blog.csdn.net/dqswuyundong/article/details/5952009 Kettle集群 Kettle是一款开源的ETL工具,以其高效和可扩展性而 ...
- Java for LeetCode 171 Excel Sheet Column Number
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- 基于centos搭建nginx+uwsgi运行django环境
环境: CentOS 7 nginx/1.9.12 Python 2.7.5 一:安装依赖包5 yum install zlib-devel bzip2-devel pcre-devel openss ...
- css样式,层叠顺序属性z-index
在做项目的时候,居然单击后显示的顺序一直被别的li标签压着,最后终于找到了,是css的z-index属性赋值了,值越大,显示的层就越高 详情推荐百度百科:z-index z-index是针对网页显示中 ...
- nodeJS文件路径总结
文件夹目录F:* test1* tes2* test3* test4* a.html*//例句fs.readFile('../../../a.html', function (err, html) { ...
- windows下同时安装python2与python3
由于python2与python3并不相互兼容,并且差别较大,所以有时需要同时安装,但在操作命令行时,怎么区别python2与python3呢? 1.下载并安装Python 2.7.9和Python ...
- linux多种安装包格式的安装方法
linux多种安装包格式的安装方法 一.rpm包安装方式步骤:1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd s ...