走用链栈走迷宫,我的代码如下。一直调试不成功,希望能求救
#include
#include
#define M 10
#define N 10
typedef struct stack_type{
struct block_type *top;
}stack_type;
typedef struct block_type{
int i,j;
int di;
struct block_type *next;
}block_type;
int mg[M+1][N+1]={
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
int main(void)
{
int i,j,di,find,k;
stack_type *s;
s=malloc(sizeof(stack_type));
block_type *p;
p=malloc(sizeof(block_type));
p->i=1;
p->j=1;
p->di=-1;
mg[1][1]=-1;
s->top=p;
while(s->top!=NULL){
i=p->i;j=p->j;di=p->di;
if(i==M-2&&j==N-2){
printf("The path is:\n");
for ( ;s->top!=NULL;s->top=s->top->next){
printf("\t(%d,%d)",s->top->i,s->top->j);
}
return 0;
}
find=0;
while (di
{di++;
switch(di){
case 0: i = s->top->i - 1;
j = s->top->j; break;
case 1: i = s->top->i;
j =s->top->j+1; break;
case 2: i = s->top->i + 1;
j = s->top->j; break;
case 3: i = s->top->i;
j = s->top->j - 1; break;
}
if (mg[i][j]==0){
find=1;
}
}
if(find==1){
block_type *p;
p=malloc(sizeof(block_type));
p->di=di;
p->i=i;
p->j=j;
p->di=-1;
mg[i][j]=-1;
s->top->next=p;
}
else
{
mg[s->top->i][s->top->j]=0;
block_type *temp;
temp=s->top;
s->top=s->top->next;
free(temp);
}
}
return 0;
}