Skip to content

Commit c143eed

Browse files
committed
Maze sovled with stack
1 parent a7c0462 commit c143eed

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

Maze/Maze.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
#include <iostream>
3+
#include <string>
4+
#include <stack>
5+
6+
using namespace std;
7+
8+
template <class T>
9+
class Stack : public stack <T>
10+
{
11+
public:
12+
T pop() {
13+
T tmp =stack<T>::top();
14+
stack<T>::pop();
15+
return tmp;
16+
}
17+
18+
/* data */
19+
};
20+
class Cell
21+
{
22+
public:
23+
Cell(int i=0 , int j=0) {
24+
x=i;y=j;
25+
}
26+
bool operator == (const Cell & c) const {
27+
return x==c.x && y==c.y;
28+
}
29+
private:
30+
int x,y;
31+
friend class Maze;
32+
/* data */
33+
};
34+
35+
class Maze
36+
{
37+
public:
38+
Maze();
39+
void exitMaze();
40+
private:
41+
Cell currentCell,exitCell,entryCell;
42+
const char exitMarker,entryMarker,visited,passage,wall;
43+
Stack<Cell> mazeStack;
44+
char ** store;
45+
void pushUnvisited(int,int);
46+
friend ostream & operator<< (ostream & , const Maze &);
47+
int rows,cols;
48+
49+
/* data */
50+
};
51+
Maze::Maze() : exitMarker('e'),entryMarker('m'),visited('.'),
52+
passage('0'),wall('1'){
53+
Stack<char *> mazeRows;
54+
char str[80],*s;
55+
int col,row=0;
56+
cout<<"Enter a rectangular maze using the following charaters:\nm-entry"
57+
<<"\ne-exit\n1-wall\n0-passage\nEnter one line at time;end with Ctrl-z:\n";
58+
while(cin>>str) {
59+
row++;
60+
cols=strlen(str);
61+
s=new char[cols+3];
62+
63+
mazeRows.push(s);
64+
strcpy(s+1, str);
65+
s[0]=s[cols+1]=wall;
66+
s[cols+2]='\0';
67+
if (strchr(s, exitMarker)!=0)
68+
{
69+
/* code */
70+
exitCell.x=row;
71+
exitCell.y=strchr(s, exitMarker)-s;
72+
}
73+
if (strchr(s, entryMarker)!=0)
74+
{
75+
/* code */
76+
entryCell.x=row;
77+
entryCell.y=strchr(s, entryMarker)-s;
78+
}
79+
80+
}
81+
rows=row;
82+
store=new char *[rows+2];
83+
store[0]=new char[cols+3];
84+
for (; !mazeRows.empty(); row--)
85+
{
86+
/* code */
87+
store[row]=mazeRows.pop();
88+
}
89+
store[row+1]=new char[cols+3];
90+
store[0][cols+2]=store[rows+1][cols+2]='\0';
91+
for(col=0;col<=cols+1;col++) {
92+
store[0][col]=wall;
93+
store[rows+1][col]=wall;
94+
}
95+
}
96+
97+
void Maze::pushUnvisited (int row,int col) {
98+
if (store[row][col]==passage || store[row][col]==exitMarker)
99+
{
100+
/* code */
101+
mazeStack.push(Cell(row,col));
102+
}
103+
}
104+
105+
void Maze::exitMaze() {
106+
int row,col;
107+
currentCell=entryCell;
108+
while(!(currentCell==exitCell)) {
109+
row=currentCell.x;
110+
col=currentCell.y;
111+
cout<<*this;
112+
if (!(currentCell==entryCell))
113+
store[row][col]=visited;
114+
pushUnvisited(row-1, col);
115+
pushUnvisited(row+1, col);
116+
pushUnvisited(row, col-1);
117+
pushUnvisited(row, col+1);
118+
if (mazeStack.empty())
119+
{
120+
/* code */
121+
cout<<*this;
122+
cout<<"Failure\n";
123+
return;
124+
}
125+
else
126+
currentCell=mazeStack.pop();
127+
}
128+
cout<<*this<<"sucess\n"<<endl;
129+
}
130+
131+
ostream & operator << (ostream & out,const Maze & maze) {
132+
for (int row=0;row<=maze.rows+1;row++)
133+
out<<maze.store[row]<<endl;
134+
out<<endl;
135+
return out;
136+
}
137+
int main()
138+
{
139+
Maze().exitMaze();
140+
while(1);
141+
return 0;
142+
}

0 commit comments

Comments
 (0)