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   2016.04.07 09:41

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