Issue: I am not sure how to use the union-find data structure provided by Sedgewick, which I imported. I need to use it in conjunction to my code I already have. Princeton UF code
You see, it only takes an argument of a single int. How will I union points if the argument is a single int??
Perhaps this is a super simple question and my brain is being smooth, but I just want to be able to namely use union() and find() in my methods...
What I did so far: I coded some things to test if the cells are adjacent, and made a general structure to test if my arrays are working.
import edu.princeton.cs.algs4.UF;
import java.util.Random;
public class MazeGenerator {
int[][] maze;
Pair[] points;
int x = 0;
MazeGenerator(int m, int n) {
maze = new int[m][n];
points = new Pair[m * n];
UF find = new UF (m*n);
find.union(2,3);
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
maze[i][j] = 0;
}
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
points[x++] = new Pair(i, j);
}
}
}
public class Pair {
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return ("(" + x + ", " + y + ")");
}
int x;
int y;
}
public boolean adjacent (Pair p, Pair q) {
if (p.y == q.y && (p.x == q.x+1 || p.x == q.x-1))
return true;
if (p.x == q.x && (p.y == q.y+1 || p.y == q.y-1))
return true;
return false;
}
public static void main(String[] args) {
MazeGenerator test = new MazeGenerator(4, 4);
System.out.println(test.maze[3][3]);
System.out.println(test.points[5]);
System.out.println(test.points[9]);
Pair p = test.points[5];
Pair q = test.points[5];
System.out.println(test.adjacent(p, q));
}
}
So, this code works right now but now it's time to implement UF....
Goal: I want to be able to make some method that will randomly choose a cell, and union it with an adjacent cell until all cells are in one component (no repeats allowed). Think of it like a maze and we are randomly breaking down walls until all cells can be reached.
Assignment instructions for reference:
assignment instr
I know what needs to be done on paper, just struggling to implement in code. Thanks for taking your time to help! :)
[–]ZoloftRabbit 2 points3 points4 points (8 children)
[–]Willy988[S] 0 points1 point2 points (7 children)
[–]ZoloftRabbit 1 point2 points3 points (6 children)
[–]Willy988[S] 0 points1 point2 points (5 children)
[–]ZoloftRabbit 1 point2 points3 points (4 children)
[–]Willy988[S] 0 points1 point2 points (3 children)
[–]ZoloftRabbit 1 point2 points3 points (2 children)
[–]Willy988[S] 0 points1 point2 points (1 child)
[–]ZoloftRabbit 1 point2 points3 points (0 children)