2

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.

Roxox
  • 99
  • 7
  • 1
    Does this answer your question? [A Java collection of value pairs? (tuples?)](https://stackoverflow.com/questions/521171/a-java-collection-of-value-pairs-tuples) –  Nov 14 '20 at 07:48
  • Use a LinkedList https://www.w3schools.com/java/java_linkedlist.asp#:~:text=The%20LinkedList%20class%20is%20a,both%20implement%20the%20List%20interface. – Tarik Nov 14 '20 at 07:51
  • @dratenik It seemed very confusing to me. Is there a simpler way as I mentioned above? – Roxox Nov 14 '20 at 07:53
  • @Tarik Dude as I stated that, I have to use stack for collecting coordinates. – Roxox Nov 14 '20 at 07:54
  • LinkedList has the necessary functionality to use as a stack. – Tarik Nov 14 '20 at 09:02

2 Answers2

1

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

ransh
  • 127
  • 3
  • 1
    Thank you for your answer, but it still seemed too compelling. I also developed a new solution. My new stack holds strings of type expressions ie N, W, S, E (directions). In this way, when I start reading my stack with if structures, I will be able to do my operations according to the element read. – Roxox Nov 14 '20 at 08:19
1

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--];
    }
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • 1
    Thank you for your answer. I also developed a new solution. My new stack holds strings of type expressions ie N, W, S, E (directions). In this way, when I start reading my stack with if structures, I will be able to do my operations according to the element read. – Roxox Nov 14 '20 at 08:48
  • For the initial maze arrays of directions seem to be required: from `[0,0]` there are paths E and S; for `[0,2], [0,4]` the paths are W,E,S (though W could be discarded), etc. – Nowhere Man Nov 14 '20 at 09:09