#include"stdio.h"
#include"stdlib.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define N 6
typedef struct pos
{
int x;
int y;
}pos;
pos origin={5,2};
pos terminus={4,5};
int maze[6][6]={
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 1, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0 }
};
typedef int SElemType,Status;
typedef struct
{
pos *base;
pos *top;
int stacksize;
}SqStack;
Status InitStack(SqStack *S)
{
S->base=(pos*)malloc(STACK_INIT_SIZE*sizeof(pos));
if(!S->base)return 0;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}
Status Push(SqStack *S,pos coord)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(pos *)realloc(S->base,(S->stacksize+STACKINCREMENT) *sizeof(pos));
if(!S->base)return 0;
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=coord;
return 1;
}
Status Pop(SqStack *S)
{
if(S->base==S->top)return 0;
--S->top;
return 1;
}
Status print(SqStack S)
{
printf("找到出口!\n\n");
printf("栈顶\n");
for (;S.base!=S.top;S.top--)
{
printf("[%d %d]\n", (S.top-1)->x,(S.top-1)->y);
}
printf("栈底\n\n");
//printf("路径长度为%u\n", S.top);
}//上面是头文件,运行时应添加为头文件
#include"stdio.h"
#include"Stack.h"
#include"stdlib.h"
Status Judge(pos coord,int maze[6][6])
{
if(coord.x>=0&&coord.x<=N-1&&coord.y>=0&&coord.y<=N-1&&maze[coord.x][coord.y]!=0)
return 1;
else return 0;
}
Status Sign(pos coord,int maze[6][6])
{
maze[coord.x][coord.y]=0;
return 1;
}
Status Check(pos coord)
{
if(coord.x==terminus.x&&coord.y==terminus.y)
return 1;
else return 0;
}
Status Centre(pos coord,SqStack S,int maze[6][6])
{
if(Judge(coord,maze))
{
Sign(coord,maze);
Push(&S,coord);
}
else return 0;
if(Check(coord))
{
print(S);
return 0;
}
pos east=coord;
east.y=coord.y+1;
Centre(east,S,maze);
pos south=coord;
south.x=coord.x+1;
Centre(south,S,maze);
pos west=coord;
west.y=coord.y-1;
Centre(west,S,maze);
pos north=coord;
north.x=coord.x-1;
Centre(north,S,maze);
Pop(&S);
return 0;
}
int main()
{
SqStack S;
printf("%d\n",InitStack(&S));
if(!Centre(origin,S,maze))
printf("not exit!\n");
}*///迷宫