一个求迷宫算法的结构,谁能帮我指点以下哪儿错了,实在是找不出来了

追忆 追忆 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include<iostream.h>
#include<stdlib.h>

#define STACK_SIZE 100
#define SIZE 10
int i=0,j=0;
typedef struct{
int ch;
int ord;
int a[2];
int di;
}selemtype;
typedef struct{
selemtype *base;
selemtype *top;
int stacksize;
}sqstack;
void initstack(sqstack *s)
{
s->base=(selemtype*)malloc(STACK_SIZE*sizeof(selemtype));
if(!s->base)
{
cout<<"overflow"<<endl;
exit(1);
}
s->top=s->base;
s->stacksize=STACK_SIZE;
}
void push(sqstack *s,selemtype *e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(selemtype*)realloc(s->base,(s->stacksize+STACK_SIZE)*sizeof(selemtype));
if(!s->base)
{
cout<<"overflow"<<endl;
exit(1);
}
s->top=s->base+s->stacksize;
s->stacksize+=STACK_SIZE;
}
*s->top++=*e;
}
void pop(sqstack s,selemtype &se)
{

if(s.top==s.base)
{
cout<<"error"<<endl;
return ;
}
se=*--s.top;
return ;
}

int pass(selemtype *d)
{
if(d->ch!=0&&d->ord==0)
return 1;
else
return 0;
}

//int footprint(char &ch)
//{



void destroystack(sqstack *s)
{
for(;s->top!=s->base;--s->top)
free(s->top-1);
}
void nextpos(selemtype *d,int a,selemtype &E1)
{
switch(a)
{
case '1':
*d==E1[i][j+1];

break;
case '2':
*d=E1[i+1][j];
break;
case '3':
*d=E1[i][j-1];
break;
case '4':
*d=E1[i-1][j];
break;
default :
break;
}
return ;
}
int stackempty(sqstack *s)
{
if(s->base==s->top)
return 0;
else
return 1;
}
void main()
{
char cha[10][10]={{0,0,0,0,0,0,0,0,0,0},{0,2,1,0,1,1,1,0,1,0},
{0,1,1,0,1,1,1,0,1,0},{0,1,1,1,1,0,0,1,1,0},{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,0,1,1,1,1,0},{0,1,0,1,1,1,0,1,1,0},{0,1,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,1,3,0},{0,0,0,0,0,0,0,0,0,0}};
cout<<"out put the migong:"<<endl;
for(int m=0;m!=10;++m)
{
for(int n=0;n!=10;++n)
{
cout<<cha[m][n]<<' ';
}
cout<<endl;
}
selemtype E[10][10];
for(;i!=10;++i)
{
for(;j!=10;++j)
{
E[i][j].ord=0;
E[i][j].ch=cha[i][j];
E[i][j].a[0]=i;
E[i][j].a[1]=j;
E[i][j].di=1;
}
}


sqstack s;
selemtype e;
//int curtep=1;
//char cupos;
initstack(&s);
i=0;
j=0;
//cupos=start;
e=E[i][j];
do{
if(pass(&e))
{

push(&s,&e);
if(e.ch==3)
{
cout<<"true"<<endl;

}
nextpos(&e,1,E);
}
else
{
if(!stackempty(&s))
{
pop(&s,&e);
while(e.di==4&&!stackempty(&s))
{
//markprint(e.seat);
pop(&s,&e);
}
if(e.di<4)
{
e.di++;
push(&s,&e);
nextpos(&e,e.di);
}
}
}
}while(!stackempty(&s));
cout<<"false"<<endl;
while(s.top!=s.base)
{

pop(&s,&e);
for(int m=0;m!=2;++m)
{
cout<<e.a[m]<<' ';
}
cout<<endl;
}
destroystack(&s);
}
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
追忆
数据结构与算法 33,026 成员 35,334 内容