#include "stdio.h"
#include "stdlib.h"
#define M 8
#define N 8
a[M+2][N+2]={{0,0,0,0,0,0,0,0,0,0},{0,1,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,1,0},{0,0,0,0,0,0,0,0,0,0}};
typedef struct
{ int x;
int y;
int foot;
int value;
}mg;
typedef struct snode
{
mg data;
struct snode *next;
}linkstacknode,*linkstack;
linkstack initlinkstack()
{
linkstack L;
L=NULL;
return L;
}
void Push(linkstack top,mg x)
{
linkstacknode *temp;
temp=(linkstacknode *)malloc(sizeof(linkstacknode));
if(temp==NULL)printf("Push wrong!");
temp->data=x;
temp->next=top->next;
top->next=temp;
}
void Pop(linkstack top)
{
top=top->next;
}
mg Get(linkstack top)
{
mg gz;
gz=top->data;
return gz;
}
mg initmg(int m,int n)
{
mg *L;
L=NULL;
L->x=m+1;
L->y=n+1;
L->foot=0;
L->value=a[m+1][n+1];
return *L;
}
void sdown(linkstack top,mg A[][N])
{
int x,y;
mg *a,*b;
*a=Get(top);
b=A[a->x-1][a->y];
if(b->value!=0)
{
Push(top,*b);
Search(top,A);
}
}
void sright(linkstack top,mg A[][N])
{
int x,y;
mg *a,*b;
*a=Get(top);
b=A[a->x][a->y-1];
if(b->value!=0)
{
Push(top,*b);
Search(top,A);
}
}
void sup(linkstack top,mg A[][N])
{
int x,y;
mg *a,*b;
*a=Get(top);
b=A[a->x-1][a->y-2];
if(b->value!=0)
{
Push(top,*b);
Search(top,A);
}
}
void sleft(linkstack top,mg A[][N])
{
int x,y;
mg *a,*b;
*a=Get(top);
b=A[a->x-1][a->y-2];
if(b->value!=0)
{
Push(top,*b);
Search(top,A);
}
}
void Search(linkstack top,mg A[][N])
{
int x,y;
mg *a,*g;
*a=Get(top);
while(a!=NULL)
{ a=(mg *)malloc(sizeof(mg));
*a=Get(top);
if(a->x==8&&a->y==8)
{printf("search end!");
while(a!=NULL)
{
*g=Get(top);
Pop(top);
printf("g->x,g->y\n");
}
exit(0);
}
if(a->foot==0)
{
a->foot=1;
x=a->x;
y=a->y;
if(y<8)sdown(top,A);
if(x<8)sright(top,A);
if(y>1)sup(top,A);
if(x>1)sleft(top,A);
}
else
{Pop(top);
Search(top,A);
}
}
printf("cannot find!");
}
main()
{mg *A[M][N];
mg *a;
linkstack top;
int m,n;
top=initlinkstack;
for(m=0;m<M;m++)
for(n=0;n<N;n++)
{
*A[m][n]=initmg(m,n);
}
a=A[0][0];
Push(top,*a);
Search(top,A);
}
迷宫问题,这个程序的问题好像应该是出在结构体指针上,求助!!!