AlgorithmsI Programming Assignment 1: Percolation
3种版本的答案,第一种使用virtual top and bottom site, 但有backwash的问题,解决这个问题有两种方法:
1. 使用2个WQUUF, 但会增加memory. One for checking if the system percolates(include virtual top and bottom), and the other to check if a given cell is full(only include virtual top). 而且要注意,判断site 是否open只能用boolean ,不然memory 就会超出限制。记住:选择合适的data structure 很重要!!
2. 仍然使用1个WQUUF, 但不使用virtual top and bottom site, 增加判断connect to top 和connect to bottom, 如果出现site 既connect to top 也connect to bottom, 那么percolate.
If any of the neighbors have connected to both set or (at least 1 is connected to top AND atleast 1 is connected to bottom) then set some local flag both to true
If connected to top is true set local flag top to true If connected to bottom is true set local flag bottom to true
Now after the unions with neighbors, find root of (I,j) And set its grid status to both or top or bottom.
If you do set it to both then you can also set a class variable percolatesFlag to true for use in the method percolates.
I haven't finished my implementation but it does seem like this will work.
java code
1. 有backwash
import edu.princeton.cs.algs4.WeightedQuickUnionUF; public class Percolation {
private boolean[] openSite; //if open is 1 , block 0
private int N; //create N-by-N grid
private WeightedQuickUnionUF uf;
private int top;
private int bottom; public Percolation(int N) { // create N-by-N grid, with all sites blocked
if (N <= 0) {
throw new IllegalArgumentException("N must be bigger than 0");
}
this.N = N;
uf = new WeightedQuickUnionUF(N*N + 2);
openSite = new boolean[N*N+2]; // 0 top_visual N*N+1 bottom_visual
top = 0;
bottom = N*N +1;
for (int i = 1; i <= N*N; i++) {
openSite[i] = false; //initial all sites block
}
} public void open(int i, int j) { // open site (row i, column j) if it is not open already
validateIJ(i, j);
int index = xyTo1D(i, j);
openSite[index] = true; if (i == 1) {
uf.union(index, top);
}
if (!percolates()) {
if (i == N) {
uf.union(index, bottom);
}
}
if (i < N && openSite[index+N]) {
uf.union(index, index+N);
}
if (i > 1 && openSite[index-N]) {
uf.union(index, index-N);
}
if (j < N && openSite[index+1]) {
uf.union(index, index+1);
}
if (j > 1 && openSite[index-1]) {
uf.union(index, index-1);
} } private int xyTo1D(int i, int j) {
validateIJ(i, j);
return j + (i-1) * N;
} private void validateIJ(int i, int j) {
if (!(i >= 1 && i <= N && j >= 1 && j <= N)) {
throw new IndexOutOfBoundsException("Index is not betwwen 1 and N");
}
} public boolean isOpen(int i, int j) { // is site (row i, column j) open?
validateIJ(i, j);
return openSite[xyTo1D(i, j)];
} /*A full site is an open site that can be connected to an open site in the top row
* via a chain of neighboring (left, right, up, down) open sites.
*/
public boolean isFull(int i, int j) { // is site (row i, column j) full?
validateIJ(i, j);
return uf.connected(top, xyTo1D(i, j));
} /* Introduce 2 virtual sites (and connections to top and bottom).
* Percolates iff virtual top site is connected to virtual bottom site.
*/
public boolean percolates() { // does the system percolate?
return uf.connected(top, bottom);
} public static void main(String[] args) { // test client (optional)
}
}
2. 使用2个WQUUF
//use two WQUUF
//One way to fix this is two use two different WQUF.
//One for checking if the system percolates(include virtual top and bottom ),
//and the other to check if a given cell is full(only include virtual top). import edu.princeton.cs.algs4.WeightedQuickUnionUF; public class Percolation {
private boolean[] openSite; //if open is true , block false
private int N; //create N-by-N grid
private WeightedQuickUnionUF uf;
private WeightedQuickUnionUF ufNoBottom;
private int top;
private int bottom; public Percolation(int N) { // create N-by-N grid, with all sites blocked
if (N <= 0) {
throw new IllegalArgumentException("N must be bigger than 0");
}
this.N = N;
uf = new WeightedQuickUnionUF(N*N + 2);
ufNoBottom = new WeightedQuickUnionUF(N*N + 1);
openSite = new boolean[N*N+2]; // 0 top_visual N*N+1 bottom_visual
top = 0;
bottom = N*N +1;
for (int i = 1; i <= N*N; i++) {
openSite[i] = false; //initial all sites block
}
} public void open(int i, int j) { // open site (row i, column j) if it is not open already
validateIJ(i, j);
int index = xyTo1D(i, j);
openSite[index] = true; if (i == 1) {
uf.union(index, top);
ufNoBottom.union(index, top);
}
if (!percolates()) {
if (i == N) {
uf.union(index, bottom);
}
}
if (i < N && openSite[index+N]) {
uf.union(index, index+N);
ufNoBottom.union(index, index+N);
}
if (i > 1 && openSite[index-N]) {
uf.union(index, index-N);
ufNoBottom.union(index, index-N);
}
if (j < N && openSite[index+1]) {
uf.union(index, index+1);
ufNoBottom.union(index, index+1);
}
if (j > 1 && openSite[index-1]) {
uf.union(index, index-1);
ufNoBottom.union(index, index-1);
}
} private int xyTo1D(int i, int j) {
validateIJ(i, j);
return j + (i-1) * N;
} private void validateIJ(int i, int j) {
if (!(i >= 1 && i <= N && j >= 1 && j <= N)) {
throw new IndexOutOfBoundsException("Index is not betwwen 1 and N");
}
} public boolean isOpen(int i, int j) { // is site (row i, column j) open?
validateIJ(i, j);
return openSite[xyTo1D(i, j)];
} /*A full site is an open site that can be connected to an open site in the top row
* via a chain of neighboring (left, right, up, down) open sites.
*/
public boolean isFull(int i, int j) { // is site (row i, column j) full?
validateIJ(i, j);
return ufNoBottom.connected(top, xyTo1D(i, j));
} /* Introduce 2 virtual sites (and connections to top and bottom).
* Percolates iff virtual top site is connected to virtual bottom site.
*/
public boolean percolates() { // does the system percolate?
return uf.connected(top, bottom);
} public static void main(String[] args) { // test client (optional)
}
}
3. 最佳方法,增加flag, 只使用1个WQUUF
//use one WQUUF to avoid backwash
import edu.princeton.cs.algs4.WeightedQuickUnionUF; public class Percolation {
private boolean[] open; //blocked: false, open: true
private boolean[] connectTop;
private boolean[] connectBottom;
private int N; //create N-by-N grid
private WeightedQuickUnionUF uf;
private boolean percolateFlag; public Percolation(int N) { // create N-by-N grid, with all sites blocked
if (N <= 0) {
throw new IllegalArgumentException("N must be bigger than 0");
}
this.N = N;
uf = new WeightedQuickUnionUF(N*N);
open = new boolean[N*N];
connectTop = new boolean[N*N];
connectBottom = new boolean[N*N]; for (int i = 0; i < N*N; i++) {
open[i] = false;
connectTop[i] = false;
connectBottom[i] = false;
}
percolateFlag = false;
} public void open(int i, int j) { // open site (row i, column j) if it is not open already
validateIJ(i, j);
int index = xyTo1D(i, j);
open[index] = true; //open
boolean top = false;
boolean bottom = false; if (i < N && open[index+N]) {
if (connectTop[uf.find(index+N)] || connectTop[uf.find(index)] ) {
top = true;
}
if (connectBottom[uf.find(index+N)] || connectBottom[uf.find(index)] ) {
bottom = true;
}
uf.union(index, index+N);
}
if (i > 1 && open[index-N]) {
if (connectTop[uf.find(index-N)] || connectTop[uf.find(index)] ) {
top = true;
}
if (connectBottom[uf.find(index-N)] || connectBottom[uf.find(index)] ) {
bottom = true;
}
uf.union(index, index-N);
}
if (j < N && open[index+1]) {
if (connectTop[uf.find(index+1)] || connectTop[uf.find(index)] ) {
top = true;
}
if (connectBottom[uf.find(index+1)] || connectBottom[uf.find(index)] ) {
bottom = true;
}
uf.union(index, index+1);
}
if (j > 1 && open[index-1]) {
if (connectTop[uf.find(index-1)] || connectTop[uf.find(index)] ) {
top = true;
}
if (connectBottom[uf.find(index-1)] || connectBottom[uf.find(index)] ) {
bottom = true;
}
uf.union(index, index-1);
}
if(i == 1) {
top = true;
}
if(i == N){
bottom = true;
}
connectTop[uf.find(index)] = top;
connectBottom[uf.find(index)] = bottom;
if( connectTop[uf.find(index)] && connectBottom[uf.find(index)]) {
percolateFlag = true;
}
} private int xyTo1D(int i, int j) {
validateIJ(i, j);
return j + (i-1) * N -1;
} private void validateIJ(int i, int j) {
if (!(i >= 1 && i <= N && j >= 1 && j <= N)) {
throw new IndexOutOfBoundsException("Index is not betwwen 1 and N");
}
} public boolean isOpen(int i, int j) { // is site (row i, column j) open?
validateIJ(i, j);
return open[xyTo1D(i, j)];
} /*A full site is an open site that can be connected to an open site in the top row
* via a chain of neighboring (left, right, up, down) open sites.
*/
public boolean isFull(int i, int j) { // is site (row i, column j) full?
validateIJ(i, j);
return connectTop[uf.find(xyTo1D(i, j))];
} /* Introduce 2 virtual sites (and connections to top and bottom).
* Percolates iff virtual top site is connected to virtual bottom site.
*/
public boolean percolates() { // does the system percolate?
return percolateFlag;
} public static void main(String[] args) { // test client (optional)
}
}
Reference:
1. http://tech-wonderland.net/blog/avoid-backwash-in-percolation.html
AlgorithmsI Programming Assignment 1: Percolation的更多相关文章
- Programming Assignment 1: Percolation
问题描述可以详见:http://coursera.cs.princeton.edu/algs4/assignments/percolation.html 关于QuickFindUF的javadoc:h ...
- AlgorithmsI Programming Assignment 1: PercolationStats.java
import edu.princeton.cs.algs4.StdOut; import edu.princeton.cs.algs4.StdRandom; import edu.princeton. ...
- Coursera Algorithms Programming Assignment 1: Percolation(100分)
题目来源http://coursera.cs.princeton.edu/algs4/assignments/percolation.html 作业分为两部分:建立模型和仿真实验. 最关键的部分就是建 ...
- 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 3.Programming Assignment : Planar data classification with a hidden layer
Planar data classification with a hidden layer Welcome to the second programming exercise of the dee ...
- Algorithms: Design and Analysis, Part 1 - Programming Assignment #1
自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming ass ...
- Algorithms : Programming Assignment 3: Pattern Recognition
Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recogniti ...
- Programming Assignment 2: Randomized Queues and Deques
实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...
- 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 2、编程作业常见问题与答案(Programming Assignment FAQ)
Please note that when you are working on the programming exercise you will find comments that say &q ...
- Programming Assignment 5: Kd-Trees
用2d-tree数据结构实现在2维矩形区域内的高效的range search 和 nearest neighbor search.2d-tree有许多的应用,在天体分类.计算机动画.神经网络加速.数据 ...
随机推荐
- iOS中__block 关键字的底层实现原理
在 <iOS面试题集锦(附答案)> 中有这样一道题目: 在block内如何修改block外部变量?(38题)答案如下: 默认情况下,在block中访问的外部变量是复制过去的,即:写操作不对 ...
- xcode升级或者重新安装后不能编译的解决方法
昨天由于xcode有一些问题,因此进行了重新安装,结果安装好后进行编译,没有进行任何改动的代码出现了两个fatal error 查看错误信息为什么的header has allready build, ...
- c++矩阵运算
优化了一些算法 #pragma once #include <iostream> #include <iomanip> #include <string> #def ...
- asp.net 自动遍历实体类
最近做项目需要读取修改前数据库中被修改的数据所有的信息,一开始想要在model层的每个类都写一个函数return一串字符串, 但是由于表太多,实体类数量太大,写出来太浪费时间,所以决定写一个通用的方法 ...
- 段落排版--行间距, 行高(line-height)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- UIDatePikcer的基本用法
- (void)viewDidLoad { [super viewDidLoad]; _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectM ...
- javascript基础学习(三)
javascript之运算符 学习要点: 表达式 运算符:一元运算符,算术运算符,关系运算符,逻辑运算符,*位运算符,赋值运算符 一.表达式 表达式有常量表达式,变量表达式,复合表达式. 二.算术运算 ...
- 文字和表单(checkbox/radio)元素垂直对齐方法,兼容Firefox和IE。
这几天在做表单时总会碰到复选框(checkbox)和单选框(radio)与文字不对齐的问题,要不是checkbox上浮了,要不是文字上浮.在前端开发过程中,单(复)选框和它们后面的提示文字在不进行任何 ...
- IOS DLNA PlatinumKit库的使用
前段时间进行了IOS DLNA的开发,使用的是PlatinumKit库.网上查了很多资料都未果,经过自己的摸索,遂将如何使用PlatinumKit进行DLNA的开发分享给大家. 1.PlatinumK ...
- sass编译css(转自阮一峰)
一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一 ...