33,010
社区成员
发帖
与我相关
我的任务
分享
#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);
}