各位达人 帮帮忙~~~一个迷宫问题的算法 实在不知道哪里出错了 编译都没错 但是执行时 总是出错 谢谢啦

eagledame 2008-03-27 02:22:24
#include <stdio.h>
typedef int status;
#include <malloc.h>
typedef struct{//posation
int x;
int y;
}PoseType;
typedef struct //stack元素
{
int ord;
PoseType seat;
int di;
}SElemType;
typedef struct//栈
{
SElemType *top;
SElemType *base;
int stacksize;
}SqStack;
//迷宫数组
int maze1[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}};;
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}};

/*for(int i=0;i<10;i++)//复制数组
{
for(int j=0;j<10;j++)
maze1[i][j]=maze[i][j];
}*/
status InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(100*sizeof(SElemType));
if(S.top==S.base) return false;
S.base=S.top;
return true;
}//初始化栈function
status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)return false;
e=*--S.top;
return true;
}//弹出栈function
status Push(SqStack &S,SElemType e)
{
e=*(S.top-1);
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))return true;
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("迷宫路径是%d,%d,%d",e.seat.x,e.seat.y);

}//弹出栈function
}
...全文
151 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
eagledame 2008-03-28
  • 打赏
  • 举报
回复
喔~~谢谢了啊 受益匪浅
mjf_2008 2008-03-27
  • 打赏
  • 举报
回复
关注..学习
wwoo_1105 2008-03-27
  • 打赏
  • 举报
回复
这么复杂啊
sheenl 2008-03-27
  • 打赏
  • 举报
回复
麻烦, 先用标准库里的stack, 看看是不是好的, 就可以看看是迷宫代码有问题还是栈代码有问题, 再找吧.

这么多代码, 不帮你看了.
ttlyfast 2008-03-27
  • 打赏
  • 举报
回复
啊 哈
Supper_Jerry 2008-03-27
  • 打赏
  • 举报
回复
几个函数你应当传引用,结果你使用的是传值。
对于结构体中有指针的情况,传值的话,只是地址值相同。
内存中数据只有一份,当其中任何一个被释放后,再次使用程序就会crash掉。
ryfdizuo 2008-03-27
  • 打赏
  • 举报
回复

//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
}
ryfdizuo 2008-03-27
  • 打赏
  • 举报
回复

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掉,
//可以运行了,没结果,那你就调试吧,
ryfdizuo 2008-03-27
  • 打赏
  • 举报
回复
status Push(SqStack &S,SElemType e)    
{
e=*(S.top-1); //函数定义有问题;
return true;
}
ryfdizuo 2008-03-27
  • 打赏
  • 举报
回复
PoseType InitBeginPos(PoseType& begin)   //传引用啊;
{
begin.x=1;begin.y=1;

return begin;
}//确定迷宫初始结束位置
PoseType InitOverPos(PoseType& over)
{
over.x=8;over.y=8;
return over;
}
lin_style 2008-03-27
  • 打赏
  • 举报
回复
这种问题。 。得花点时间。。。
最好是自己单步跟踪着调。。
ouyh12345 2008-03-27
  • 打赏
  • 举报
回复
单步调试

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧