这个题目就是给定一个迷宫数组,让走下来。应用链栈。
我的思想是:先判断走的路是否正确,0是正确,1是墙,2是终点。然后用1234分别表示移动方向,保存在elem结构体中的c里边。比如从右边走来,就是1,上边走来就是2,左边3,下边走来就是4。
然后这样一步步走,碰头就回。
但是,我在VC上编译没错。但就是不能GO。,
显示:Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
First-chance exception in mingg.exe: 0xC0000005: Access Violation.
一直都是在PUSH函数中有错,
所以没办法继续执行,不知道有没有逻辑错误
求高手帮忙看一下
#include<stdio.h>
#include<malloc.h>
typedef struct {
int a;
int b; //a,b,分别表示迷宫中当前在数组中的位置
int c; //c表示移动方向;
}elem;
typedef struct Link1
{
elem p;
struct Link1 *next; //NEXT指向前一个LINK
} link;
int creat(link *p){
p=(link *)malloc(sizeof(link));
if(!p) return -1;
p->next=NULL;
p->p.a=0;
p->p.b=0;
p->p.c=0;
printf("created.\n");
return 1;
}
int pop(link *p,elem *e){
if(p->next==0)return -1;
*e=p->p;
p=p->next;
return 1;
}
int push(link *g,elem e){
link *q;
q=(link *)malloc(sizeof(link));
if(q==NULL)return -1;
q->p.a=e.a;
q->p.b=e.b;
q->p.c=e.c;
q->next=g->next;
g->next=q;
return 1;
}
int sousou(link *road,int (*a)[10],int i,int j,int k){
int (*q)[10];
q=a;
if(*(*(q+i)+j)==0)
{ int m;
elem p,*r;
p.a=i;
p.b=j;
p.c=k;
push(road,p);
switch(k){
case 1:if(!(m=sousou(road,a,i,j-1,1))) //1,2,3,4表示移动方向,1表示左,2下,3右,4上
if(!(m=sousou(road,a,i+1,j,2)))
m=sousou(road,a,i-1,j,4);break;
case 2:if(!(m=sousou(road,a,i,j-1,1)))
if(!(m=sousou(road,a,i+1,j,2)))
m=sousou(road,a,i,j+1,3);break;
case 3:if(!(m=sousou(road,a,i+1,j,2)))
if(!(m=sousou(road,a,i,j+1,3)))
m=sousou(road,a,i-1,j,4);break;
case 4:if(!(m=sousou(road,a,i,j-1,1)))
if(!(m=sousou(road,a,i+1,j,2)))
m=sousou(road,a,i-1,j,4);break;
}if(m==2)return 2;
else if(m==0) {pop(road,r); return 0;}
else return 1;
}
else if(*(*(q+i)+j)==2) return 2;
else return 0;
}
int main()
{
elem *p;
link *s;
int m,i;
int a[][10]={
1,0,1,1,1,1,1,1,1,1, //迷宫数组
1,0,0,1,0,0,0,1,0,1,
1,0,0,0,0,1,1,0,1,1,
1,0,1,1,1,0,0,1,0,1,
1,0,0,0,1,0,0,0,0,1,
1,0,1,0,0,0,1,0,1,1,
1,0,1,1,1,1,0,0,1,1,
1,1,1,0,0,0,1,0,1,1,
1,1,1,0,0,0,0,0,0,2,
1,1,1,1,1,1,1,1,1,1
};
if(!creat(s)) return -1;
m=sousou(s,a,0,1,2);
if(!m)
printf("error\n");
else{
for(i=1;s->next==NULL;i++){
pop (s,p);
printf("(%d,%d,%d)\n",p->a,p->b,p->c);
}
}
getchar();
return 0;
}