I'm writing an algorithm that solves a maze, and I have a maze called char [] [] maze. Its elements be like;
{1,1,1,1,1,1, ..},
{1,0,1,0,1,1, ..},
{1,0,0,1,0,1, ..}, ...
There are 13 Rows and 17 Columns. I have to solve it using the chunk data structure. According to the algorithm I have set up in my mind, I need to store the index values of the navigable path in this stack. For example according to the above maze:
0,0
0,1
0,2
0,3
0,4
1,4
1,5
2,5...
I used to keep an integer number in my previous examples, so I used a structure like this when implementing stack construction.
public class Stack {
int topOfStack;
int capacity;
int[] Stack;
public Stack(int capacity) {
this.capacity = capacity;
Stack = new int[capacity];
topOfStack = -1;
}
void push(int element)
{
if(topOfStack == capacity){
System.out.println("Stack Overflow...");
}
else{
topOfStack++;
Stack[topOfStack] = element;
}
}
}
My question is exactly this. How can I modify this stack structure for my maze solver program? If I need to state it again, I have to keep coordinates or something similar in stack, not integers. Thanks a lot.
You can create a new class object which called Coordinates. This class will have two main parameters which are X and Y. Then you can make a stuck that is filled with this Coordinates object instead of a simple Integer.
The stuck you are building can be generic and contain a more complex structures then the basic one and this is what you are looking in this example
To put it simply, a 2D array could be used to store the coordinates:
public class Stack {
int topOfStack;
int capacity;
int[][] stack;
public Stack(int capacity) {
this.capacity = capacity;
stack = new int[capacity][2];
topOfStack = -1;
}
void push(int x, int y)
{
if(topOfStack == capacity){
System.out.println("Stack Overflow...");
}
else{
stack[++topOfStack] = new int[] { x, y };
}
}
int[] pop() {
if (topOfStack < 0) {
System.out.println("Stack is empty");
return null;
}
return stack[topOfStack--];
}
}