stacks in maze

Jan 13, 2009 at 2:21am
Im a noob in c++
I dont know how to use stack in this maze?,the stack will be the one who will store the paths and I want the program to not use recursion,how can I do that in this program?

a similar algorithm for this one is listed below,but what I did is so different from this algo

Find the starting point
look for the letters along the border of your maze
while stack is not empty
pop stack,store it in a variable as current row and column except for visited path
check for a path on all sides ofthe current postion
if there is path,push it in the stack
loop


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

   
#include <stdlib.h>				// allows use of = clrscreen();
#include <conio.h>
#include <fstream>
#include <iostream.h>
#include <iomanip> 

#include <stack.h>

#include "clear.h"


using namespace std;
void Traverse( char[][80], int, int, int);
void Maze(const char[][80]);
bool validmove(const char[][80],int,int);               
bool Edge (int,int);
enum direction {DOWN,RIGHT,UP,LEFT};  //0,1,2,3
const int x = 2;
const int y = 0;
////****Main****////

 void pushX(int);
 void pushY(int);
 

int main()
   {
       char maze[25][80]; 
                    
                        
                        
                         
                       
   ifstream infile;
   infile.open("25X80_1.txt");
 
    for (int i = 0; i < 25; i++) {
         for (int j = 0; j < 80; j++) {
                     infile>>maze[i][j];   
                   }
                    //cout<<'\n'; 
            }
    
    Traverse(maze,x,y,RIGHT);                       
        
    system("PAUSE");
    return 0;
}

 
void Traverse(char maze[][80], int xlocation, int ylocation, int direction)  
 {
       
        stack< int > s;       
        
        
    	maze[xlocation][ylocation]='Ü';   
       
       cout<<"Determining current state "<<endl<<"Array"; 
       pushX(xlocation);
       pushY(ylocation);
        
       cout<<endl<<endl;
                    
       
         
       //popY(ylocation);
    //  popX(xlocation);  
       
         cout<<endl<<endl;
        
     	Maze(maze); 
     	 clrscreen();
       // pushArray();   
           
           
           
    if (Edge(xlocation,ylocation) && xlocation!=x && ylocation!=y)  
         {
        	cout<<"Maze game SOLVED!!! \n";  
               
            return;
          }
               else {
             	
                  	for (int move =direction,count=0;count<4;count++,move++,move%=4)   
                  
                      switch (move)   

                                       	{
                                	case DOWN:
                	                          	if ( validmove(maze,xlocation+1,ylocation) ) 
                                                    {
                    	                                Traverse(maze,xlocation+1,ylocation,LEFT);
                	                                       return;
                                                        }
                                                      break;
                                     
                                     
                                     
                                     
                                 	case RIGHT:
                	                        	if (validmove(maze,xlocation,ylocation+1))
                                                    {
                    	                               	Traverse(maze,xlocation,ylocation+1,DOWN);
                                                           return;
                                                       }

                                                       	break;
                                                                   	
                                   	case UP:
                		                       if (validmove(maze,xlocation-1,ylocation))
                                                  {
                    	                                Traverse(maze,xlocation-1,ylocation,RIGHT);
                    	                                 return;
                                                           }
                                                     	break;
                                                                       	
                                                                       	
                	               case LEFT:
                		                      if (validmove(maze,xlocation,ylocation-1))
                                                    {
                    	                             	Traverse(maze,xlocation,ylocation-1,UP);
                                                         return;
                                                     }
                                                     	break;
                	               	}    //end switch
                              	}      //end for loop
                             }    //end Travers function




           
     bool validmove(const char maze[][80],int r, int c)    
              {
                return (r>=0 && r<=24 && c>=0 && c<=79 && maze[r][c]!='*');       
              }

    

     bool Edge(int x ,int y)   
            {
               if ((x==0||x==24)&&(y>=0&&y<=79))
                return true;
                else if ((y==0||y==79)&&(x>=0&&x<=24)) 
                return true;
                else
                return false;
            }

      
      
     void Maze(const char maze[][80]) {

      
             


         for (int x=0;x<25;x++)  {
                    for (int y=0;y<80;y++)  {
                         cout<<maze[x][y];    
                        }  
                   cout<<'\n';
                }

                // cout<<"Press Enter for the next move\n";
               cin.get();
                 
            }

     void pushX(int x ){  //push row
       
           stack< int > s;   
            s.push( x );
          cout <<"["<<s.top()<<"]";
           
         }
     
     
     void pushY(int y ){    //push column
        stack< int > s;   
   
        s.push( y );
        cout <<"["<<s.top()<<"]";
      
}     
 
    
Last edited on Jan 13, 2009 at 12:39pm
Jan 13, 2009 at 2:35am
If I understand correctly what you're trying to do, you want s, which is an std::stack<int> to be available to all your functions.
You need to pass s by reference:
void pushX(std::stack<int> &s,int x);

Those for loops in lines 175 and 185 are pretty much pointless.

Though you say do don't want to use recursion, Traverse() is recursive.
Jan 13, 2009 at 12:51pm
line 175 and 185 forgot to erase that,haha,my mistake

yes Traverse() was in a recursive way but I want someone to do that in a simply procedural way(is that the right a term??) and it uses a stack

Jan 13, 2009 at 2:05pm
One maze-solving algorithm, Tremaux's algorithm, can be used without recursion.
http://en.wikipedia.org/wiki/Maze#Solving_mazes

PS: Algorithms are usually divided into recursive and iterative.
Last edited on Jan 13, 2009 at 2:09pm
Jan 13, 2009 at 10:38pm
ok thanks for the idea,I got it all covered right now.
Topic archived. No new replies allowed.