C语言走迷宫的问题,自学搞了2晚还是搞不懂,求大神路过瞄2眼
Shilyx/MazeScrnSave · GitHub看这个完整的例子 【C语言走迷宫的问题,自学搞了2晚还是搞不懂,求大神路过瞄2眼】 
■网友
我感觉你还是先看一下这个比较好:AskForHelp - Woodpecker Wiki for CPUG一上来就贴大段代码, 还没有格式, 我相信99.99%的人只瞟一眼鼠标就到右上去角了.
■网友
下面这个是自己写的程序****************************************************************************************************************#include\u0026lt;stdio.h\u0026gt;#include\u0026lt;stdlib.h\u0026gt;typedef struct walk{int ord; int di;int x,y;struct walk *next;}S,*s;typedef struct QP{struct walk *top;struct walk *botton;}stack;void print(int *a)//打印迷宫和路线 {int i,j;for(i=0;i\u0026lt;10;i++){for(j=0;j\u0026lt;10;j++){if(*a==1)printf("□");if(*a==0)printf("■"); if(*a==2)printf("卍");a++;}printf("\");}}bool pass(int (*p),int a,int b){if(p==0)return true;else return false;}bool out(int a,int b)//判断当前位置是不是出口 {if(a==8\u0026amp;\u0026amp;b==8)return true;else return false;}bool bol(int (*p),S *now)//判断当前位置是不是通路 {int a,b;now-\u0026gt;di=1;while(1){if(now-\u0026gt;di==1){a=now-\u0026gt;x;b=now-\u0026gt;y+1;if(pass(p,a,b))return true;else now-\u0026gt;di++;}if(now-\u0026gt;di==2){b=now-\u0026gt;y;a=now-\u0026gt;x+1;if(pass(p,a,b))return true;else now-\u0026gt;di++;}if(now-\u0026gt;di==3){a=now-\u0026gt;x;b=now-\u0026gt;y-1;if(pass(p,a,b))return true;else now-\u0026gt;di++;}if(now-\u0026gt;di==4){b=now-\u0026gt;y;a=now-\u0026gt;x-1;if(pass(p,a,b))return true;else return false;}}}void insert(s pas,stack *t)//入栈 {s p=(s)malloc(sizeof(S));p-\u0026gt;ord=pas-\u0026gt;ord;p-\u0026gt;di=pas-\u0026gt;di;p-\u0026gt;x=pas-\u0026gt;x;p-\u0026gt;y-pas-\u0026gt;y;p-\u0026gt;next=t-\u0026gt;top;t-\u0026gt;top=p;}bool gonext(int (*p),S *now,int *c,int *d)//走向下一位置为当前位置 {int a,b;a=*c;b=*d;now-\u0026gt;di=1;while(1){if(now-\u0026gt;di==1){a=now-\u0026gt;x;b=now-\u0026gt;y+1;if(pass(p,a,b)){*c=a;*d=b;now-\u0026gt;x=a;now-\u0026gt;y=b;return true;}else now-\u0026gt;di++;}if(now-\u0026gt;di==2){b=now-\u0026gt;y;a=now-\u0026gt;x+1;if(pass(p,a,b)){*c=a;*d=b;now-\u0026gt;x=a;now-\u0026gt;y=b;return true;}else now-\u0026gt;di++;}if(now-\u0026gt;di==3){a=now-\u0026gt;x;b=now-\u0026gt;y-1;if(pass(p,a,b)){*c=a;*d=b;now-\u0026gt;x=a;now-\u0026gt;y=b;return true;}else now-\u0026gt;di++;}if(now-\u0026gt;di==4){b=now-\u0026gt;y;a=now-\u0026gt;x-1;if(pass(p,a,b)){*c=a;*d=b;now-\u0026gt;x=a;now-\u0026gt;y=b;return true;}}}}void del(stack *t)//出栈 {stack *p=t;t-\u0026gt;top=t-\u0026gt;top-\u0026gt;next;free(p);}bool empty(stack *t)//判断是不是空栈 {if(t-\u0026gt;top==t-\u0026gt;botton)return true;else return false;}void pop(stack *t)//出栈 {stack *p=t;t-\u0026gt;top=t-\u0026gt;top-\u0026gt;next;free(p);}int main(){int a={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};print(a);stack R;int c,d;//当前位置坐标 R.top=R.botton=(s)malloc(sizeof(S));R.top-\u0026gt;next=NULL;S start,end;c=start.x=1;d=start.y=1;start.ord=1;start.di=1;//入口位置和当前位置的赋值 end.x=8;end.y=8;//出口坐标 while(1){if(out(c,d))//判断是不是出口 {a=2;//记录足迹 print(a);exit(0);//打印迷宫和路线图 }else{if(bol(a,\u0026amp;start))//判断当前位置是否通路 {a=2;start.ord++;insert(\u0026amp;start,\u0026amp;R);//入栈 gonext(a,\u0026amp;start,\u0026amp;c,\u0026amp;d);//进入下一位置 }else{a=3;if(!empty(\u0026amp;R))//判断栈是否非空 {pop(\u0026amp;R);start.ord=R.top-\u0026gt;ord;start.di=R.top-\u0026gt;di;c=start.x=R.top-\u0026gt;x;d=start.y=R.top-\u0026gt;y;}else{printf("失败");exit(1);}}}}}
推荐阅读
- 江苏■江苏交控坚持问题导向、瞄准职工需求——找准“病灶”当好“产改先行官”
- 贵州在建骨干水源工程达到465座有效解决工程性区域性缺水问题
- 四川眉山瓦屋山景区就游客投诉、停车难等问题公开道歉
- 杭州已整改城市道路无障碍环境问题12467处
- 互联网怎样解决“家政服务上门速度慢”的问题
- 中东问题|
- 中国网汽车|购车2个多月、仅行驶8000多公里 宝骏730遭遇7处问题
- OC为何跌出语言榜前十
- |沛县深入开展教育领域突出问题专项整改
- 交换机,路由器经常性的死机咋办
