Maze with Stack.

In class been asked to create a program which the following 'S' is guided to 'E' in this 2D maze.

‘S’ is the starting position and ‘E’ is the destination. ‘X’ stands for wall which is not allowed to move into.
Each time S can make a move in either one of the four directions, i.e. east, south, west and north, as long as it is a blank square.

This needs to be done using Stack but I have no clue where to even start...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char arrMaze [SIZE][SIZE] = {/*0*/ {'X', 'X', 'X', 'X', 'X', 'X','X', 'X', 'X','X', 'X', 'X','X', 'X', 'X'},
/*1*/ {'X', 'S', 'X', 'X', 'X', 'X',' ', ' ', ' ',' ', 'X', 'X','X', 'X', 'X'},
/*2*/ {'X', ' ', ' ', 'X', ' ', ' ',' ', 'X', 'X',' ', 'X', ' ',' ', ' ', 'X'},
/*3*/ {'X', 'X', ' ', 'X', 'X', ' ','X', 'X', 'X',' ', ' ', ' ','X', ' ', 'X'},
/*4*/ {'X', ' ', ' ', 'X', 'X', ' ',' ', ' ', ' ','X', ' ', ' ',' ', ' ', 'X'},
/*5*/ {'X', ' ', 'X', 'X', 'X', 'X','X', ' ', ' ','X', 'X', 'X',' ', 'X', 'X'},
/*6*/ {'X', ' ', 'X', ' ', 'X', 'X','X', ' ', ' ','X', ' ', ' ',' ', 'X', 'X'},
/*7*/ {'X', ' ', 'X', ' ', ' ', ' ',' ', ' ', 'X',' ', ' ', 'X','X', 'X', 'X'},
/*8*/ {'X', ' ', ' ', ' ', 'X', 'X','X', ' ', 'X',' ', 'X', ' ',' ', ' ', 'X'},
/*9*/ {'X', 'X', ' ', ' ', 'X', 'X','X', ' ', 'X',' ', 'X', ' ','X', ' ', 'X'},
/*0*/ {'X', 'X', ' ', 'X', ' ', ' ',' ', ' ', 'X',' ', 'X', ' ','X', ' ', 'X'},
/*1*/ {'X', 'X', ' ', 'X', ' ', ' ',' ', 'X', 'X',' ', 'X', ' ','X', ' ', 'X'},
/*2*/ {'X', 'X', ' ', 'X', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ','X', ' ', 'X'},
/*3*/ {'X', 'X', ' ', 'X', 'X', 'X','X', 'X', 'X','X', 'X', ' ','X', 'E', 'X'},
/*4*/ {'X', 'X', 'X', 'X', 'X', 'X','X', 'X', 'X','X', 'X', 'X','X', 'X', 'X'}
you generally want to try the next move in the same order each time. It helps, at first.
say you try N, then E, then S, then W from each place.
you need to be able to see your previous move, so your stack needs a look at top capability.

so here,
1)look at previous, empty stack.
try N, can't
try E, can't
try south, can, push S
--
1) look at previous, its S, so north is illogical
try east, ok, push E
---
look at previous.
... and so on.
when you reach a point that you tried west and cannot proceed, you pop the stack. This needs new logic! You have to look at what you popped off. If you popped of E, for example, continue by checking S and W, if you hit W and cannot, pop again..

so it slowly eliminates failed paths and converges on the first working path it can find by brute force. Don't forget to check that you hit the exit square after each move.

Topic archived. No new replies allowed.