do
{
if(Pass(curpos))//当前位置可以通过,即是未曾走到的通道块
{
FootPrint(curpos);//留下足迹
e.ord=curstep;
e.seat=curpos;
e.di=1;
Push(S,e);//加入路径
if(curpos.x==end.x&&curpos.y==end.y)
{
cout<< "\n终点("<< e.seat.y<< ","<< e.seat.x<< ")";
returnTRUE;//到达终点(出口)
}
curpos=NextPos(curpos,e.di);//下一位置是当前位置的东邻
++curstep;//探索下一步
}
else//当前位置不能通过
{
if(!
StackEmpty(S))
{
Pop(S,e);
while(e.di==4&&!
StackEmpty(S))
{
MakePrint(e.seat);//留下不能通过的标记
Pop(S,e);
cout<< "倒退到("<< e.seat.y<< ","<< e.seat.x<< ")";
}
if(e.di< 4)
{
++e.di;//换下一个方向探索
Push(S,e);
curpos=NextPos(e.seat,e.di);//设定当前位置是该新方向上的相邻块
}
}
}
}while(!
StackEmpty(S));
returnFALSE;
}\\2009100136
5)探索下一个位置
PosTypeNextPos(PosTypecurPos,int&i)
{
switch(i)//顺时针方向
{
case1:
++curPos.x;//东
if(mazeMap[curPos.y][curPos.x]!
=2)
break;
--curPos.x;
case2:
i=2;
++curPos.y;//南
if(mazeMap[curPos.y][curPos.x]!
=2)
break;
--curPos.y;
case3:
i=3;
--curPos.x;//西
if(mazeMap[curPos.y][curPos.x]!
=2)
break;
++curPos.x;
case4:
i=4;
--curPos.y;//北
if(mazeMap[curPos.y][curPos.x]==2)
{
++curPos.y;
mazeMap[curPos.y][curPos.x]=0;
}
break;
}
returncurPos;
}\\2009100132
6)标记走过的路径
voidFootPrint(PosTypepos)
{
mazeMap[pos.y][pos.x]=2;//将走过的路径设为2
}\\2009100132
7)标记作废路径
voidMakePrint(PosTypepos)
{
cout<< "\n("<< pos.y<< ","<< pos.x<< ")走不通,作废";
mazeMap[pos.y][pos.x]=0;//将走不通的块替换为墙壁
}\\2009100132
8)函数调用
intmain()
{
PosTypemazeStart,mazeEnd;
mazeStart.x=1;//开始与结束点
mazeStart.y=1;
mazeEnd.x=8;
mazeEnd.y=8;
cout<< "迷宫:
"<< endl;
for(inti=0;i< 10;++i)
{
for(intj=0;j< 10;++j)
cout<< mazeMap[i][j];
cout<< endl;
}
cout<< endl<< endl;
if(MazePath(mazeStart,mazeEnd))
cout<< "\n走通迷宫"<< endl;
else
cout<< "\n走不通迷宫"<< endl;
system("PAUSE");
return0;
}\\2009100122
五 调试分析
1、在编程序时迷宫的出入栈掌握的还好,可是编到迷宫方向的选择,与迷宫路径错误的情况时,不知道怎么处理,然后自己通过看书和网上查阅资料基本解决了问题。
2、在写代码的过程中,没有弄清使用指针与引用之后,结构体如何使用。
当使用指针的时候要使用‘.’,当使用引用或数的时候,要使用‘->’。
六 使用说明
首先由用户输入一组二维数组来组成迷宫,确认后程序自动运行,
1、当迷宫有完整路径可以通过时,以0和1所组成的迷宫形式输出,标记所走过的路径结束程序;
2、当迷宫无路径时,提示输入错误结束程序。
程序执行的命令:
1创建迷宫;2求解迷宫;3输出迷宫求解;
七 测试结果
八 实验总结
1.本次实验利用了关于栈的相关知识,入栈、出栈、判断栈满、增加存储空间等等,使学习到的知识能够很好的结合利用,在实际的操作中加强熟练程度。
2.对于没有掌握好的知识,应该及时的参考课本或者网络资源,使知识落实到实处,不留空白。
3.关于迷宫中行走方向的选择这一块需要重点的练习,函数的运用不够灵活。
九 模块分工
2009100145 辛志鹏 入栈、出栈和判断栈是否为空
2009100136 张发辉 迷宫路径求解
2009100122 赵金桃 调用函数
2009100132 田 飞 探索下一位置、标记走过和作废的路径
附完整代码:
#defineOK1
#defineERROR0
#defineTRUE1
#defineFALSE0
#defineOVERFLOW-2
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
typedefintStatus;
typedefstruct//迷宫中x行y列的位置
{
intx;
inty;
}PosType;
typedefstruct//栈类型
{
intord; //通道块在路径上的“序号”
PosTypeseat;//通道块在迷宫中的“坐标位置”
intdi; //从此通道块走向下一通道块的“方向”,//1:
东2:
北3:
西(顺时针)
}MazeType;
typedefstruct
{
MazeType*base;
MazeType*top;
intstacksize;
}MazeStack;
#include
usingnamespacestd;
StatusInitStack(MazeStack&S);
StatusPush(MazeStack&S,MazeType&e);
StatusPop(MazeStack&S,MazeType&e);
StatusStackEmpty(MazeStack&S);
StatusMazePath(PosTypestart,PosTypeend);
StatusPass(PosType&pos);
voidFootPrint(PosTypepos);
PosTypeNextPos(PosTypecurPos,int&i);
voidMakePrint(PosTypepos);
//迷宫地图,0表示墙壁,1表示通路,入口:
mazeMap[1][1],出口mazeMap[8][8]
intmazeMap[10][10]=
{ //0,1,2,3,4,5,6,7,8,9
{0,0,0,0,0,0,0,0,0,0},//0
{0,1,1,0,1,1,1,0,1,0},//1
{0,1,1,0,1,1,1,0,1,0},//2
{0,1,1,1,1,0,0,1,1,0},//3
{0,1,0,0,0,1,1,1,1,0},//4
{0,1,1,1,0,1,1,1,1,0},//5
{0,1,0,1,1,1,0,1,1,0},//6
{0,1,0,0,0,1,0,0,1,0},//7
{0,0,1,1,1,1,1,1,1,0},//8
{0,0,0,0,0,0,0,0,0,0} //9
};
intmain()
{
PosTypemazeStart,mazeEnd;
mazeStart.x=1;//开始与结束点
mazeStart.y=1;
mazeEnd.x=8;
mazeEnd.y=8;
cout<< "迷宫:
"<< endl;
for(inti=0;i< 10;++i)
{
for(intj=0;j< 10;++j)
cout<< mazeMap[i][j];
cout<< endl;
}
cout<< endl<< endl;
if(MazePath(mazeStart,mazeEnd))
cout<< "\n走通迷宫"<< endl;
else
cout<< "\n走不通迷宫"<< endl;
system("PAUSE");
return0;
}
StatusInitStack(MazeStack&S)//新建一个栈
{
S.base=(MazeType*)malloc(STACK_INIT_SIZE*sizeof(MazeType));
if(!
S.base)
exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
returnOK;
}
StatusPush(MazeStack&S,MazeType&e)//入栈
{
if(S.top-S.base>=S.stacksize)
{
S.base=(MazeType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(MazeType));
if(!
S.base)
exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
returnOK;
}
StatusPop(MazeStack&S,MazeType&e)//出栈
{
if(S.top==S.base)
returnERROR;
e=*--S.top;
returnOK;
}
StatusStackEmpty(MazeStack&S)//判断是否为空
{
if(S.base==S.top)
returnOK;
returnERROR;
}
StatusMazePath(PosTypestart,PosTypeend)//迷宫路径求解
{
PosTypecurpos;
MazeStackS;
MazeTypee;
intcurstep;
InitStack(S);
curpos=start;//设定当前位置为入口位置
curstep=1;//探索第一步
cout<< "起点:
"<< "("< do
{
if(Pass(curpos))//当前位置可以通过,即是未曾走到的通道块
{
FootPrint(curpos);//留下足迹
e.ord=curstep;
e.seat=curpos;
e.di=1;
Push(S,e);//加入路径
if(curpos.x==end.x&&curpos.y==end.y)
{
cout<< "\n终点("<< e.seat.y<< ","<< e.seat.x<< ")";
returnTRUE;//到达终点(出口)
}
curpos=NextPos(curpos,e.di);//下一位置是当前位置的东邻
++curstep;//探索下一步
}
else//当前位置不能通过
{
if(!
StackEmpty(S))
{
Pop(S,e);
while(e.di==4&&!
StackEmpty(S))
{
MakePrint(e.seat);//留下不能通过的标记
Pop(S,e);
cout<< "倒退到("<< e.seat.y<< ","<< e.seat.x<< ")";
}
if(e.di< 4)
{
++e.di;//换下一个方向探索
Push(S,e);
curpos=NextPos(e.seat,e.di);//设定当前位置是该新方向上的相邻块
}
}
}
}while(!
StackEmpty(S));
returnFALSE;
}
StatusPass(PosType&pos)
{
if(mazeMap[pos.y][pos.x]==0)
returnFALSE;
cout<< "->("<< pos.y<< ","<< pos.x<< ")";
returnTRUE;
}
voidFootPrint(PosTypepos)
{
mazeMap[pos.y][pos.x]=2;//将走过的路径设为2
}
PosTypeNextPos(PosTypecurPos,int&i)
{
switch(i)//顺时针方向
{
case1:
++curPos.x;//东
if(mazeMap[curPos.y][curPos.x]!
=2)
break;
--curPos.x;
case2:
i=2;
++curPos.y;//南
if(mazeMap[curPos.y][curPos.x]!
=2)
break;
--curPos.y;
case3:
i=3;
--curPos.x;//西
if(mazeMap[curPos.y][curPos.x]!
=2)
break;
++curPos.x;
case4:
i=4;
--curPos.y;//北
if(mazeMap[curPos.y][curPos.x]==2)
{
++curPos.y;
mazeMap[curPos.y][curPos.x]=0;
}
break;
}
returncurPos;
}
voidMakePrint(PosTypepos)
{
cout<< "\n("<< pos.y<< ","<< pos.x<< ")走不通,作废";
mazeMap[pos.y][pos.x]=0;//将走不通的