70,037
社区成员
发帖
与我相关
我的任务
分享
//OK了,上面的代码省了,
status InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(100*sizeof(SElemType));
if(S.top==S.base) return false;
//S.base=S.top;
S.top=S.base;
S.stacksize=0;
return true;
}//初始化栈function
status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)return false;
e=*--S.top;
S.stacksize--;
return true;
}//弹出栈function
status Push(SqStack& S, SElemType e)
{
// e=*(S.top-1);
*S.top = e;
S.top++;
S.stacksize++;
return true;
}//入栈function
PoseType NextPos(PoseType pos,int di)
{
switch(di)
{
case 1: pos.y++;break;
case 2: pos.x++;break;
case 3: pos.y--;break;
case 4: pos.x--;break;
}return pos;
}
void FootPrint(PoseType pos)
{
maze1[pos.x][pos.y]=2;
}
bool Pass(PoseType pos)
{
if (maze1[pos.x][pos.y]==0)
return true;
else return false;
}
bool Gameover(PoseType curposation,PoseType endpos)
{
if (curposation.x==endpos.x&&curposation.y==endpos.y)
return true;
else return false;
}
void MarkPrint(PoseType pos)
{
maze1[pos.x][pos.y]=3;
}
PoseType InitBeginPos(PoseType& begin)
{
begin.x=1;begin.y=1;
return begin;
}
PoseType InitOverPos(PoseType& over)
{
over.x=8;over.y=8;
return over;
}
status StackEmpty(SqStack S)
{
if(S.base==S.top)return true;
else return false;
}
status main()
{
SqStack S;
PoseType start;PoseType end;PoseType curpos;
int curstep=1;
SElemType e;
InitBeginPos(start);
InitOverPos(end);
curpos=start;
InitStack(S);//
do
{
if(Pass(curpos))//如果路可通
{
FootPrint(curpos);
e.ord=curstep;e.seat=curpos;e.di=1;
Push(S,e);
if( Gameover(curpos,end))
break; //不能够return,否则就推出main,不会输出了;
curpos=NextPos(curpos,1);
curstep++;
}
else
{
Pop(S,e);
while (e.di==4)
{
MarkPrint(e.seat);Pop(S,e);
}//while
if(e.di <4)
{
e.di++;Push(S,e);
curpos=NextPos(e.seat,e.di);
}//if
}//else
}while(!StackEmpty(S));
//去掉return false;
while(S.top!=S.base)
{
e=*--S.top;
printf("迷宫路径是%4d, %4d\n",e.seat.x,e.seat.y); //多了一个%d
}//弹出栈function
}
status InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(100*sizeof(SElemType));
if(S.top==S.base) return false;
//S.base=S.top; 原来你这里将已经申请号的base指向的对空间又丢弃了,错.
S.top=S.base;
S.stacksize=0;
return true;
}
//注意程序结束后内存释放,将malloc申请的空间free掉,
//可以运行了,没结果,那你就调试吧,
status Push(SqStack &S,SElemType e)
{
e=*(S.top-1); //函数定义有问题;
return true;
}PoseType InitBeginPos(PoseType& begin) //传引用啊;
{
begin.x=1;begin.y=1;
return begin;
}//确定迷宫初始结束位置
PoseType InitOverPos(PoseType& over)
{
over.x=8;over.y=8;
return over;
}