c语言程序设计的迷宫这个可行的/4.3.3源程序/includeincludeincludeincludeincludedefineN20/迷宫的大小,可改变/intoldmapNN;/递归用的数组,用全局变量节约时间/intyes=0;/yes是判断是否找到路的标志,1找到,0没找到/intway1002,wayn=0;/way数组是显示路线用的,wayn是统计走了几个格子/voidInit(void);/图形初始化/voidClose(void);/图形关闭/voidDrawPeople(intx,inty,intn);/画人工
探索物图/voidPeopleFind(int(x)N);/人工探索/voidWayCopy(int(x)N,int(y)N);/为了8个方向的递归,把旧迷宫图拷贝给新数组/intFindWay(int(x)N,inti,intj);/自动探索函数/voidMapRand(int(x)N);/随机生成迷宫函数/voidPrMap(int(x)N);/输出迷宫图函数/voidResult(void);/输出结果处理/voidFind(void);/成功处理/voidNotFind(void);/失败处理/voidmain(void)/主函数/intmapNN;/迷宫数组/charch;clrscr();printf("\nPleaseselecthand(1)elseauto\n");/选择探索方式/scanf("%c",ch);Init();/初始化/MapRand(map);/生成迷宫/PrMap(map);/显示迷宫图/if(ch==1)PeopleFind(map);/人工探索/FindWay(map,1,1);/系统自动从下标1,1的地方开始探索/Result();/输出结果/Close();voidInit(void)/图形初始化/intgd=DETECT,gm;initgraph(gd,gm,"c:\\tc");voidDrawPeople(intx,inty,intn)/画人工控制图/{/如果将以下两句注释掉,则显示人工走过的路径,/setfillstyle(SOLIDFILL,WHITE);/设置白色实体
填充样式/bar(100+(y)15-6,50+(x)15-6,100+(y)15+6,50+(x)15+6);/恢复原通路/switch(n)/判断x,y的变化,8个方向的变化/case1:(x)--;break;/上/case2:(x)--;(y)++;break;/右上/case3:(y)++;break;/右/case4:(x)++;(y)++;break;/右下/case5:(x)++;break;/下/case6:(x)++;(y)--;break;/左下/case7:(y)--;break;/左/case8:(x)--;(y)--;break;/左上/setfillstyle(SOLIDFILL,RED);/新位置显示探索物/bar(100+(y)15-6,50+(x)15-6,100+(y)15+6,50+(x)15+6);voidPeopleFind(int(map)N)/人工手动查找/intx,y;charc=0;/接收按键的变量/x=y=1;/人工查找的初始位置/setcolor(11);line(500,200,550,200);outtextxy(570,197,"d");line(500,200,450,200);outtextxy(430,197,"a");line(500,200,500,150);outtextxy(497,130,"w");line(500,200,500,250);outtextxy(497,270,"x");line(500,200,450,150);outtextxy(445,130,"q");line(500,200,550,150);outtextxy(550,130,"e");line(500,200,450,250);outtextxy(445,270,"z");line(500,200,550,250);outtextxy(550,270,"c");/以上是画8个方向的控制介绍/setcolor(YELLOW);outtextxy(420,290,"PressEnterto");/压回车键结束/setfillstyle(SOLIDFILL,RED);bar(100+y15-6,50+x15-6,100+y15+6,50+x15+6);/
入口位置显示/while(c!=13)/如果按下的不是回车键/c=getch();/接收字符后开始各个方向的探索/if(c==wmapx-1y!=1)DrawPeople(x,y,1);/上/if(c==emapx-1y+1!=1)DrawPeople(x,y,2);/右上/if(c==dmapxy+1!=1)DrawPeople(x,y,3);/右/if(c==cmapx+1y+1!=1)DrawPeople(x,y,4);/右下/if(c==xmapx+1y!=1)DrawPeople(x,y,5);/下/if(c==zmapx+1y-1!=1)DrawPeople(x,y,6);/左下/if(c==amapxy-1!=1)DrawPeople(x,y,7);/左/elseif(c==qmapx-1y-1!=1)DrawPeople(x,y,8);/左上/setfillstyle(SOLIDFILL,WHITE);/消去红色探索物,恢复原迷宫图/bar(100+y15-6,50+x15-6,100+y15+6,50+x15+6);if(x==N-2y==N-2)/人工控制找成功的话/yes=1;/如果成功标志为1/voidWayCopy(int(oldmap)N,int(map)N)/拷贝迷宫数组/inti,j;for(i=0;i