资源描述:
《栈和递归的应用,迷宫算法.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#defineM6#defineN8move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//方向坐标值maze[M+2][N+2]={{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,1,0,1,0,1,1},{1,1,0,0,1,1,0,0,0,1},{1,0,0,0,0,0,0,1,1,1},{1,1,1,0,1,1,0,0,0,1},{1,0,0,0,0,0,1,0,1,1},{1,1,0,1,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1}};mark[M+2][N+2]={{
2、0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}};//寻找迷宫路径intPeekPath(intx,inty){inti;//行走方向introw,col;//下一步的行坐标值、列坐标值if(x==M&&y==N)//到达终点则返回1结束return(1);for(i=0;i<4
3、;i++){row=x+move[i][0];col=y+move[i][1];if(maze[row][col]==0&&mark[row][col]==0)//所在位置可走且未标记则标记该位置并判断是否通向终点路径{mark[row][col]=1;if(PeekPath(row,col)==1)//路径通向终点则打印路径坐标{printf("(%d,%d)",row,col);return(1);}}}return(0);}voidmain(){inti,j;printf("迷宫图:");for(i=0;i4、f(maze[i][j]==1)printf("%d",1);elseprintf("");}printf("");}printf("");mark[1][1]=1;printf("迷宫图解法:");if(PeekPath(1,1)==1)printf("(1,1)");printf("");printf("计算机走向痕迹:");for(i=0;i