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

追忆 2008-03-21 05:52:38
#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);
}
...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
meiZiNick 2008-05-01
  • 打赏
  • 举报
回复
我也想知道,正在找這方面的資料~~~~~
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
等待牛人来答.
UltraBejing 2008-04-30
  • 打赏
  • 举报
回复
有问题请先GOOGLE,BAIDU
追忆 2008-03-24
  • 打赏
  • 举报
回复
怎么输出的迷宫不对
追忆 2008-03-22
  • 打赏
  • 举报
回复
我就是不懂哪错了
追忆 2008-03-22
  • 打赏
  • 举报
回复
能具体解释一下吗
lzy340623339 2008-03-22
  • 打赏
  • 举报
回复
#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[][10])
{
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,E);
}
}
}
}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);
}
kgn28 2008-03-21
  • 打赏
  • 举报
回复
把程序提出来应该不会有什么人来给你一条一条分析吧!
我建议你用vc好好调试一下,捋顺一下思路,不要急。

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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