下图为测试代码的图例(对应main函数的maze数组):
//严蔚敏《数据结构》
//栈应用:迷宫求解,之前在家的时候不理解,现在终于可以敲出代码了,555
//自学中,加油啊!!!
#include<iostream>
using namespace std;
typedef struct
{
int x;
int y;
}PosType;
typedef struct
{
int ord;
PosType seat;
int di;
}SElemType;
typedef struct SNode
{
SElemType data;
struct SNode* next,* prior;
}SNode,* Link;
typedef struct
{
Link head,tail;
int len;
}LinkStack;
bool Init_Stack(LinkStack& s)
{
s.head=new SNode;
if(!s.head)
return false;
s.head->next=s.head->prior=nullptr;
s.tail=s.head;
s.len=0;
return true;
}
void Output_Stack(LinkStack s)
{
Link p=s.head->next;
cout<<"路径:"<<endl;
while(p){
cout<<"第"<<p->data.ord<<"步:("<<p->data.seat.x<<","<<p->data.seat.y<<")\n";
p=p->next;
}
cout<<"===="<<endl;
}
bool IsEmpty_Stack(LinkStack s)
{
if(s.len)
return false;
return true;
}
bool Push_Stack(LinkStack& s,SElemType e)
{
Link p=new SNode;
if(!p)
return false;
p->data=e;
p->next=s.tail->next;
p->prior=s.tail;
s.tail->next=p;
s.tail=p;
s.len++;
return true;
}
bool Pop_Stack(LinkStack& s,SElemType& e)
{
Link p;
e=s.tail->data;
p=s.tail;
s.tail=s.tail->prior;
free(p);
s.tail->next=nullptr;
s.len--;
return true;
}
bool Pass(int maze[10][10],PosType curpos)
{
if(maze[curpos.x][curpos.y]==0)
return true;
return false;
}
void FootPrint(int maze[10][10],PosType curpos)
{
maze[curpos.x][curpos.y]=9;
}
PosType NextPos(PosType curpos,int m)
{
PosType nextPos=curpos;
switch(m)
{
case 1:nextPos.y++;break;//bug1:1!='1' bug2:137行代码de e.seat不允许动
case 2:nextPos.x++;break;
case 3:nextPos.y--;break;
case 4:nextPos.x--;
}
return nextPos;
}
void MarkPrint(int maze[10][10],PosType curpos)
{
maze[curpos.x][curpos.y]=1;
}
void MazePathPrint(int maze[10][10])
{
cout<<"迷宫为:(其中9为可通路径)\n";
for(int i=0;i!=10;i++){
for(int j=0;j!=10;j++)
cout<<maze[i][j]<<' ';
cout<<endl;
}
cout<<endl;
}
bool MazePath(int maze[10][10],PosType start,PosType end)
{
LinkStack s;
Init_Stack(s);
PosType curpos=start;
int curstep=1;
SElemType e;
do{
if(Pass(maze,curpos)){
e.ord=curstep;e.di=1;e.seat=curpos;
Push_Stack(s,e);
FootPrint(maze,e.seat);
if(e.seat.x==end.x&&e.seat.y==end.y){
Output_Stack(s);
MazePathPrint(maze);
return true;
}
curpos=NextPos(e.seat,e.di);
curstep++;
}
else{
if(!IsEmpty_Stack(s)){
Pop_Stack(s,e);
while(e.di==4&&!IsEmpty_Stack(s)){
MarkPrint(maze,e.seat);
curstep--;
Pop_Stack(s,e);
}
if(e.di<4){
e.di++;Push_Stack(s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}while(!IsEmpty_Stack(s));
return false;
}
int main()
{
int maze[10][10]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
PosType start,end;
start.x=start.y=1;
end.x=end.y=8;
MazePath(maze,start,end);
return 0;
}
路径:
第1步:(1,1)
第2步:(1,2)
第3步:(2,2)
第4步:(3,2)
第5步:(3,1)
第6步:(4,1)
第7步:(5,1)
第8步:(5,2)
第9步:(5,3)
第10步:(6,3)
第11步:(6,4)
第12步:(6,5)
第13步:(7,5)
第14步:(8,5)
第15步:(8,6)
第16步:(8,7)
第17步:(8,8)
====
迷宫为:(其中9为可通路径)
1 1 1 1 1 1 1 1 1 1
1 9 9 1 1 1 1 1 0 1
1 0 9 1 1 1 1 1 0 1
1 9 9 1 1 1 1 0 0 1
1 9 1 1 1 0 0 0 0 1
1 9 9 9 1 0 0 0 0 1
1 0 1 9 9 9 1 0 0 1
1 0 1 1 1 9 1 1 0 1
1 1 0 0 0 9 9 9 9 1
1 1 1 1 1 1 1 1 1 1
Process returned 0 (0x0) execution time : 0.096 s
Press any key to continue.