Java Maze Building: Find the Edges of an hxw Grid of Squares

In summary, the conversation discusses finding the edges shared by cells in a grid and the attempts at creating a method to generate these edges. The code provided includes a loop to iterate through the rows and columns of the grid, with conditions to check for valid edges and add them to an ArrayList. Some adjustments may be needed for different grid sizes.
  • #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;
	}
I'm looping over the row, then dropping down one level and looping over the next row, etc. As I find an edge, it is placed into a two element array and then stored in the arraylist to be return and processed.

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.
 
Physics news on Phys.org
  • #2



Hi there,

It looks like you're on the right track with your code, but you may need to adjust your loop conditions and edge calculations to account for different grid sizes. For example, in a 2x2 grid, the edge [0,1] is located at index 2, but in a 3x3 grid, it would be located at index 4. You may also need to consider how the index changes as you move to a new row or column in the grid.

One possible solution could be to use nested for loops, with one loop iterating through the rows and the other iterating through the columns. Within each loop, you can check if there is a valid edge to be added and then add it to the ArrayList if so. You may also need to consider the order in which you add the edges, as it may affect the output.

I hope this helps and good luck with your project!
 

1. What is Java Maze Building?

Java Maze Building is the process of creating a maze with Java programming language. It involves writing code to generate a maze with a specified height and width, and finding the edges of the maze.

2. What is an hxw grid of squares?

An hxw grid of squares is a grid made up of h rows and w columns of squares. This type of grid is commonly used in maze building to represent the layout of the maze.

3. How do you find the edges of a Java maze?

To find the edges of a Java maze, you need to use algorithms such as depth-first search or breadth-first search. These algorithms traverse the maze and mark the edges as they encounter them.

4. Why is it important to find the edges of a maze?

Finding the edges of a maze is important because it helps in solving the maze. The edges serve as boundaries that the solver must not cross, which makes it easier to navigate through the maze.

5. Can you build a Java maze with any size?

Yes, you can build a Java maze with any size, as long as it is within the memory and processing limits of your computer. However, larger mazes may take longer to generate and solve.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
841
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top