- #1
Trentonx
- 39
- 0
Homework Statement
So, I'm writing a class that builds a maze using disjoints sets, and need to find the edges shared by the cells in the grid.
Find the edges, not including boundaries, of an h by w grid of squares.
For example a 2x2 grid
+--+--+
| 0 |1 |
+--+--+
| 2 |3 |
+--+--+
has the edges [0,1], [0,2], [1,3], [2,3]
Homework Equations
errm?
The Attempt at a Solution
The relevant method
Code:
public static ArrayList<int[]> generateEdges(int h, int w) {
ArrayList<int[]> t = new ArrayList<int[]>();
int[] edge;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if ((j + 2 * i + 1) < w * (i + 1)) {
edge = new int[2];
edge[0] = j + 2 * i;
edge[1] = j + 2 * i + 1;
t.add(edge);
}
if ((j + 2 * i + h) < (j + i + (2 * h) - 1)) {
edge = new int[2];
edge[0] = j + 2 * i;
edge[1] = j + 2 * i + h;
t.add(edge);
}
// see if right is clear
// add right if true
// see if below is clear
// add if it is
// +2i seems relevant
}
}
return t;
}
The problem is with the tests to ensure I don't get an edge which is not adjacent or directly below. They code as it is works for a 2x2 grid, but fails for anything else. Any pointers on this would be greatly appreciated.