用栈的迷宫问题
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 40
#define M 10
#define N 10
typedef struct
{
int x,y,d;
}guide;
typedef struct
{
guide data[MAXNUM];
int top;
}STACK;
typedef struct
{
int x,y;
}item;
item move[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int maze[M][N]={1,1,1,1,1,1,1,1,1,1,
1,0,1,1,0,0,1,0,0,1,
1,1,0,0,1,1,0,1,0,1,
1,1,1,0,1,1,0,1,1,1,
1,1,0,1,1,1,1,0,0,1,
1,0,1,1,0,0,1,1,0,1,
1,0,1,0,1,1,0,0,0,1,
1,0,1,1,0,1,1,1,1,1,
1,1,0,1,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1};
STACK *Init_stack()
{
STACK *s;
s=(STACK *)malloc(sizeof(STACK));
s->top=-1;
return s;
}
int empty_stack(STACK *s)
{
if(s->top==-1) return 1;
else return 0;
}
void Push_stack(STACK *s,guide temp)
{
if(s->top==MAXNUM-1) puts("no space");
else
{
s->top++;
s->data[s->top].x=temp.x;
s->data[s->top].y=temp.y;
s->data[s->top].d=temp.d;
}
}
void Pop_stack(STACK *s,guide temp)
{
if(empty_stack(s)) printf("empty stack\n");
else
{
temp.x=s->data[s->top].x;
temp.y=s->data[s->top].y;
temp.d=s->data[s->top].d;
s->top--;
}
}
main()
{
STACK *s;
guide temp;
int x,y,i,j,d;
s=Init_stack();
temp.x=1;
temp.d=-1;
temp.y=1;
Push_stack(s,temp);
while(!empty_stack(s))
{
printf("%d,%d,%d\n",s->data[s->top].x,s->data[s->top].y,s->data[s->top].d);
Pop_stack(s,temp);
x=temp.x;
y=temp.y;
d=temp.d+1;
while(d<8)
{
i=x+move[d].x;
j=y+move[d].y;
if(maze[i][j]==0)
{
printf("%d--%d\n",i,j);
temp.d=d;
temp.x=x;
temp.y=y;
Push_stack(s,temp);
printf("%d,%d,%d\n",s->data[s->top].x,s->data[s->top].y,s->data[s->top].d);
x=i;y=j;maze[x][y]=69;
if(x==8&&y==x)
{
printf("out!\n");
exit(0);
}
else d=0;
}
else d++;
}
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
printf("%d\t",maze[i][j]);
printf("\n");
}
}
我已经是看了又看,对了又对书本。可就是得不出正确结果。高手帮忙啊