2 cluoluoya Cluoluoya 于 2016.04.07 09:36 提问

求能帮忙改改迷宫的代码

图片说明
走用链栈走迷宫,我的代码如下。一直调试不成功,希望能求救
#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;

}

1个回答

CSDNXIAON
CSDNXIAON   2016.04.07 09:41

迷宫代码
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!