Question: Hello! This is a basic percolation program that I've coded for my homework in Java. The program mostly works, however, when I run it the
Hello! This is a basic percolation program that I've coded for my homework in Java. The program mostly works, however, when I run it the square at position 0,1 is ALWAYS opened. Can you help me figure out why? I'm not sure what I'm doing wrong.

package algs15.perc;
//import stdlib.*; import algs15.*;
// Uncomment the import statements above.
// You can test this using InteractivePercolationVisualizer and PercolationVisualizer // All methods should make at most a constant number of calls to the UF data structure, // except percolates(), which may make up to N calls to the UF data structure.
// You can use a second UF to answer percolates. For the second UF, you can connect // all of the elements of the top row and separately all of the elements of the bottom row. // Then you can implement percolates as a single call to "connected".
public class Percolation { int N; boolean[] open; // TODO: more fields to add here int top = N*N; WeightedUF WUF; public Percolation(int N) { this.N = N; this.open = new boolean[N*N]; // TODO: more to do here this.WUF = new WeightedUF(N*N); } // open site (row i, column j) if it is not already public void open(int i, int j) { int position = i*N+j; open[position] = true; // TODO: more to do here. 4 cases. (Is is connect on both the top and the bottom?) if (i == 0) { WUF.union(position, top); } // Check left if (i > 0 && isOpen(i-1, j)) { WUF.union(position, (i-1)*N+j); } // Check right if (i 0 && isOpen(i, j-1)) { WUF.union(position, i*N+(j-1)); } // Check below if (j Standard Draw File 1 open sites does not percolate
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
