- with the code that i currently have. I need to know the best way to create a c
ID: 3664130 • Letter: #
Question
- with the code that i currently have. I need to know the best way to create a connection betwee clicked squares and the top row so that those scuares that are clicked which are touching the top row or another connected square will also show that it is connected and become a blue square instead of a white square.. im currently working on that through the public void open(int i, int j) method
i will attach all the code and classes that i currently have
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation {
public boolean [][] gridPopulate;
int top;
public int [][] gridNumberSystem;
WeightedQuickUnionUF weightedQuickUnion;
private Object[][] check;
public Percolation(int N)
{
boolean testUnion;
gridPopulate = new boolean[N][N];
gridNumberSystem = new int[N][N];
weightedQuickUnion = new WeightedQuickUnionUF((N * N)+2);
int top = (N * N)+ 1;
int x=1;
for(int row=0; row<N; row++)
{
for(int column =0; column < gridNumberSystem[0].length; column++)
{
gridNumberSystem[row][column] = x;
System.out.println(gridNumberSystem[row][column]);
x++;
}
}
for(int i =0; i <= N; i++)
{
weightedQuickUnion.union(i, top);
}
//);
}
// create NbyN grid, with all sites blocked
public void open(int i, int j)
{
int k =getNumberSystemValue(i, j);
gridPopulate[i][j]= true;
System.out.println("gridNumberSys" +gridNumberSystem[i][j]);
System.out.println("this could be it " + getNumberSystemValue(i, j));
if(isOpen(i,j))
{
if (weightedQuickUnion.connected(1, top))
{
weightedQuickUnion.union(gridNumberSystem[i][j], top);
}
// if (gridNumberSystem[i][j] <100 && isOpen(i-1,j) )
// {
// weightedQuickUnion.union(gridNumberSystem[i][j], getNumberSystemValue(i-1, j));
// }
{
//System.out.printf("Col: %d Col -1: %d%n", i, j);
}
// if (flatGridSquare[row][col].getRow() > 0 && isOpen(row -1, col))
// {
// weightedQUickUnionUF.union(getFlatgridNum(row, col), getFlatgridNum(row - 1, col)); //check going down will add - 1
// System.out.printf("Row: %d Row -1: %d%n", row, row -1);
// }
}
// open site (row i, column j) if it is not open already
}
public boolean isOpen(int i, int j)
{
return gridPopulate[i][j];
}
public boolean isFull(int i, int j)
{
int q = getNumberSystemValue(i, j);
if (isOpen(i, j))
{
return weightedQuickUnion.connected(getNumberSystemValue(i, j), top);
}
else return false;
// is site (row i, column j) full?
}
public boolean percolates()
{
return false;
}
public String numberOfOpenSites() {
// TODO Auto-generated method stub
return null;
}
public void connectSquare(int i, int j)
{
if(isOpen(0,5) == true )
{
weightedQuickUnion.union(15, top);
}
else
System.out.println("hell no");
}
public int getNumberSystemValue(int i, int j)
{
return gridNumberSystem[i][j];
}
// public int getRow(int row, int column)
// {
// int check [][];
// check[row][column];
// return row;
// }
//
}
--------------------------------------------------------------------------
/******************************************************************************
* Compilation: javac InteractivePercolationVisualizer.java
* Execution: java InteractivePercolationVisualizer N
* Dependencies: PercolationVisualizer.java Percolation.java
*
* This program takes the grid size N as a command-line argument.
* Then, the user repeatedly clicks sites to open with the mouse.
* After each site is opened, it draws full sites in light blue,
* open sites (that aren't full) in white, and blocked sites in black.
*
******************************************************************************/
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
public class InteractivePercolationVisualizer {
private static final int DELAY = 20;
public static void main(String[] args) {
// N-by-N percolation system (read from command-line, default = 10)
int N = 10;
if (args.length == 1) N = Integer.parseInt(args[0]);
// turn on animation mode
StdDraw.show(0);
// repeatedly open site specified my mouse click and draw resulting system
StdOut.println(N);
Percolation perc = new Percolation(N);
PercolationVisualizer.draw(perc, N);
StdDraw.show(DELAY);
while (true) {
// detected mouse click
if (StdDraw.mousePressed()) {
// screen coordinates
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
// convert to row i, column j
int i = (int) (N - Math.floor(y) - 1);
int j = (int) (Math.floor(x));
// open site (i, j) provided it's in bounds
if (i >= 0 && i < N && j >= 0 && j < N) {
if (!perc.isOpen(i, j)) {
StdOut.println(i + " " + j);
}
perc.open(i, j);
}
// draw N-by-N percolation system
PercolationVisualizer.draw(perc, N);
}
StdDraw.show(DELAY);
}
}
}
-------------------------------------------------------------------------------------
/******************************************************************************
* Compilation: javac PercolationVisualizer.java
* Execution: java PercolationVisualizer input.txt
* Dependencies: Percolation.java
*
* This program takes the name of a file as a command-line argument.
* From that file, it
*
* - Reads the grid size N of the percolation system.
* - Creates an N-by-N grid of sites (intially all blocked)
* - Reads in a sequence of sites (row i, column j) to open.
*
* After each site is opened, it draws full sites in light blue,
* open sites (that aren't full) in white, and blocked sites in black,
* with with site (0, 0) in the upper left-hand corner.
*
******************************************************************************/
import java.awt.Font;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
public class PercolationVisualizer {
// delay in miliseconds (controls animation speed)
private static final int DELAY = 100;
// draw N-by-N percolation system
public static void draw(Percolation perc, int N) {
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setXscale(-.05*N, 1.05*N);
StdDraw.setYscale(-.05*N, 1.05*N); // leave a border to write text
StdDraw.filledSquare(N/2.0, N/2.0, N/2.0);
// draw N-by-N grid
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (perc.isFull(row, col)) {
StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
}
else if (perc.isOpen(row, col)) {
StdDraw.setPenColor(StdDraw.WHITE);
}
else {
StdDraw.setPenColor(StdDraw.BLACK);
}
StdDraw.filledSquare(col + 0.5, N - row - 0.5, 0.45);
}
}
// write status text
StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.text(.25*N, -N*.025, perc.numberOfOpenSites() + " open sites");
if (perc.percolates()) StdDraw.text(.75*N, -N*.025, "percolates");
else StdDraw.text(.75*N, -N*.025, "does not percolate");
}
private static void simulateFromFile(String filename) {
In in = new In(filename);
int N = in.readInt();
Percolation perc = new Percolation(N);
// turn on animation mode
StdDraw.show(0);
// repeatedly read in sites to open and draw resulting system
draw(perc, N);
StdDraw.show(DELAY);
while (!in.isEmpty()) {
int i = in.readInt();
int j = in.readInt();
perc.open(i, j);
draw(perc, N);
StdDraw.show(DELAY);
}
}
public static void main(String[] args) {
String filename = args[0];
simulateFromFile(filename);
}
}
Explanation / Answer
Answer:
Note: I have modified the Percolation class.
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation
{
private int top;
private boolean[][] gridPopulate;
private WeightedQuickUnionUF weightedQuickUnion;
public int [][] gridNumberSystem;
// Create a NxN grid with all weightedQuickUnion closed
public Percolation(int N)
{
top = N;
weightedQuickUnion = new WeightedQuickUnionUF(n*n);
gridPopulate = new boolean[n][n];
gridNumberSystem = new int[N][N];
int x=1;
for(int row=0; row<N; row++)
{
for(int column =0; column < gridNumberSystem[0].length; column++)
{
gridNumberSystem[row][column] = x;
System.out.println(gridNumberSystem[row][column]);
x++;
}
}
}
// Open the weightedQuickUnion on ithrow & jth col
public void open(int i, int j)
{
int k =getNumberSystemValue(i, j);
System.out.println("gridNumberSys" +gridNumberSystem[i][j]);
System.out.println("this could be it " + getNumberSystemValue(i, j));
if (!gridPopulate[i-1][j-1])
{
connectNeighbors(i, j);
gridPopulate[i-1][j-1] = true;
}
}
// CHECK IF GIVEN weightedQuickUnion IS OPEN
public boolean isOpen(int i, int j)
{
boolean b=gridPopulate[i-1][j-1];
return b;
}
public boolean isFull(int i, int j)
{
if (!isOpen(i, j))
return false;
int q = getNumberSystemValue(i, j);
int row1 = 1;
for (int col1 = 1; col1 <= top; col1++)
{
if (isOpen(row1, col1) && weightedQuickUnion.connected(q, getNumberSystemValue(row1, col1)))
{
return true;
}
}
return false;
}
public boolean percolates()
{
int row1 = top;
for (int col1 = 1; col1 <= top; col1++)
{
if (isFull(row1, col1))
return true;
}
return false;
}
public int getNumberSystemValue(int i, int j)
{
int g=gridNumberSystem[i][j];
return g;
}
public void connectSquare(int i, int j) {
int q = getNumberSystemValue(i, j);
if (i > 1 && isOpen(i-1, j))
weightedQuickUnion.union(getNumberSystemValue(i-1, j), q);
if (j < top && isOpen(i, j+1))
weightedQuickUnion.union(getNumberSystemValue(i,j+1), q);
if (j > 1 && isOpen(i, j-1))
weightedQuickUnion.union(getNumberSystemValue(i,j-1), q);
if (i < top && isOpen(i+1, j))
weightedQuickUnion.union(getNumberSystemValue(i+1, j), q);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.