主题:[讨论]构造迷宫竟出现异常的困惑
当用户在输入完所要构造的迷宫时,按回车见就会发现构造的迷宫成了被1包围的单列迷宫,而不是我们想要得结果,麻烦各位大师帮忙看看应该杂修改,谢谢。
#include<iostream>
#include<string>
#include<stack>
using namespace std;
template<class T>
class Stack : public stack<T>
{
public:
T pop()
{
T tmp=top();
stack<T>::pop();
return tmp;
}
};
class Cell
{
public:
Cell(int i=0,int j=0)
{x=i;y=j;}
bool operator==(const Cell& c)const
{return x==c.x&&y==c.y;}
private:
int x,y;
friend class Maze;
};
class Maze
{
public:
Maze();
void exitMaze();
private:
Cell currentCell,exitCell,entryCell;
const char exitMarker,entryMarker,visited,passage,wall;
Stack<Cell> mazeStack;
char **store;
void pushunvisited(int,int);
friend ostream& operator<<(ostream&,const Maze&);
int rows,cols;
};
Maze::Maze():exitMarker('e'),entryMarker('m'),visited('.'),
passage('0'),wall('1')
{
Stack<char*> mazeRows;
char str[80],*s;
int col,row=0,i=0;
cout<<"enter a rectanger maze using the following"
<<"charcters:\nm - entry\ne -exit\n1 -wall\n0 -passage\n"
<<"ctrl+z -end\n";
while(cin>>str[i])
{
row++;
cols=i++;
s=new char[cols+3];
mazeRows.push(s);
strcpy(s+1,str);
s[0]=s[cols+1]=wall;
s[cols+2]='\0';
if(strchr(s,exitMarker)!=0)
{
exitCell.x=row;
exitCell.y=strchr(s,exitMarker)-s;
}
if(strchr(s,entryMarker)!=0)
{
exitCell.x=row;
exitCell.y=strchr(s,entryMarker)-s;
}
}
rows=row;
store=new char*[rows+2];
store[0]=new char[cols+3];
for(;!mazeRows.empty();row--)
{store[row]=mazeRows.pop();}
store[rows+1]=new char[cols+3];
store[0][cols+2]=store[rows+1][cols+2]='\0';
for(col=0;col<=cols+1;col++)
{
store[0][col]=wall;
store[rows+1][col]=wall;
}
}
void Maze::pushunvisited(int row,int col)
{
if(store[row][col]==passage||store[row][col]==exitMarker)
{mazeStack.push(Cell(row,col));}
}
void Maze::exitMaze()
{
int row,col;
currentCell=entryCell;
while(!(currentCell==exitCell))
{
row=currentCell.x;
col=currentCell.y;
cout<<*this;
if(!(currentCell==entryCell))
store[row][col]=visited;
pushunvisited(row-1,col);
pushunvisited(row+1,col);
pushunvisited(row,col-1);
pushunvisited(row,col+1);
if(mazeStack.empty())
{
cout<<*this;
cout<<"Failure\n";
return;
}
else currentCell=mazeStack.pop();
}
cout<<*this;
cout<<"success\n";
}
ostream& operator<<(ostream& out,const Maze& maze)
{
for(int row=0;row<=maze.rows+1;row++)
out<<maze.store[row]<<endl;
out<<endl;
return out;
}
int main()
{
Maze().exitMaze();
return 0;
}
#include<iostream>
#include<string>
#include<stack>
using namespace std;
template<class T>
class Stack : public stack<T>
{
public:
T pop()
{
T tmp=top();
stack<T>::pop();
return tmp;
}
};
class Cell
{
public:
Cell(int i=0,int j=0)
{x=i;y=j;}
bool operator==(const Cell& c)const
{return x==c.x&&y==c.y;}
private:
int x,y;
friend class Maze;
};
class Maze
{
public:
Maze();
void exitMaze();
private:
Cell currentCell,exitCell,entryCell;
const char exitMarker,entryMarker,visited,passage,wall;
Stack<Cell> mazeStack;
char **store;
void pushunvisited(int,int);
friend ostream& operator<<(ostream&,const Maze&);
int rows,cols;
};
Maze::Maze():exitMarker('e'),entryMarker('m'),visited('.'),
passage('0'),wall('1')
{
Stack<char*> mazeRows;
char str[80],*s;
int col,row=0,i=0;
cout<<"enter a rectanger maze using the following"
<<"charcters:\nm - entry\ne -exit\n1 -wall\n0 -passage\n"
<<"ctrl+z -end\n";
while(cin>>str[i])
{
row++;
cols=i++;
s=new char[cols+3];
mazeRows.push(s);
strcpy(s+1,str);
s[0]=s[cols+1]=wall;
s[cols+2]='\0';
if(strchr(s,exitMarker)!=0)
{
exitCell.x=row;
exitCell.y=strchr(s,exitMarker)-s;
}
if(strchr(s,entryMarker)!=0)
{
exitCell.x=row;
exitCell.y=strchr(s,entryMarker)-s;
}
}
rows=row;
store=new char*[rows+2];
store[0]=new char[cols+3];
for(;!mazeRows.empty();row--)
{store[row]=mazeRows.pop();}
store[rows+1]=new char[cols+3];
store[0][cols+2]=store[rows+1][cols+2]='\0';
for(col=0;col<=cols+1;col++)
{
store[0][col]=wall;
store[rows+1][col]=wall;
}
}
void Maze::pushunvisited(int row,int col)
{
if(store[row][col]==passage||store[row][col]==exitMarker)
{mazeStack.push(Cell(row,col));}
}
void Maze::exitMaze()
{
int row,col;
currentCell=entryCell;
while(!(currentCell==exitCell))
{
row=currentCell.x;
col=currentCell.y;
cout<<*this;
if(!(currentCell==entryCell))
store[row][col]=visited;
pushunvisited(row-1,col);
pushunvisited(row+1,col);
pushunvisited(row,col-1);
pushunvisited(row,col+1);
if(mazeStack.empty())
{
cout<<*this;
cout<<"Failure\n";
return;
}
else currentCell=mazeStack.pop();
}
cout<<*this;
cout<<"success\n";
}
ostream& operator<<(ostream& out,const Maze& maze)
{
for(int row=0;row<=maze.rows+1;row++)
out<<maze.store[row]<<endl;
out<<endl;
return out;
}
int main()
{
Maze().exitMaze();
return 0;
}