#includeiostream#includeiomanip#includefstream#includectime#includeSQ_Stack.husingnamespacestd;structVisited{intnum;//是否是通路(1或0)intvisited;//判断是否已被访问(1或0)};classMiGong{private:Visitedmaze[100][100];//存储迷宫的矩阵intn;//迷宫大小(n*n)intpow;//判断方向的变量public:voidcreat()//建造迷宫{intchoose,i,j;while(1){cout1.自动输入endl;cout2.手动输入endl;cout请输入序号:;cinchoose;system(cls);if(choose==1)//自动输入{cout请输入迷宫大小(n*n):;cinn;for(i=0;in;i++)//访问位初始化,赋值为0for(j=0;jn;j++)maze[i][j].visited=0;srand((unsigned)time(0));cout你生成的迷宫为(n*n型):endl;for(i=0;in;i++)for(j=0;jn;j++){if(i==0||j==0||i==n-1||j==n-1)//若是边界,则置为1maze[i][j].num=1;elsemaze[i][j].num=(rand()%(2-0));if(i==1&&j==1)//入口置为0maze[i][j].num=0;elseif(i==n-2&&j==n-2)//出口置为0maze[i][j].num=0;coutmaze[i][j].num;if(j==n-1)coutendl;}coutendl;break;}elseif(choose==2)//手动输入{cout请输入迷宫大小(n*n):;cinn;for(i=0;in;i++)//访问位初始化为0for(j=0;jn;j++)maze[i][j].visited=0;system(cls);while(1){intc;cout1.当前页面输入:endl;cout2.文件输入:endl;cinc;if(c==1){system(cls);cout请手动输入迷宫:endl;intmi;cout你生成的迷宫为(n*n型):endl;for(i=0;in;i++)for(j=0;jn;j++){cinmi;maze[i][j].num=mi;coutmaze[i][j].num;if(j==n-1)coutendl;}coutendl;coutendl;break;}elseif(c==2){ifstreaminfile(输入迷宫.txt);for(i=0;in;i++)for(j=0;jn;j++){infilemaze[i][j].num;coutmaze[i][j].num;if(j==n-1)coutendl;}infile.close();break;}else{cout输入错误,请重新输入!;system(pause);system(cls);continue;}}break;}else{cout输入错误,请重新输入!;system(pause);system(cls);continue;}}}voidseek()//寻找路径{SQ_StackStack;//定义栈Stack.InitStack();//栈初始化if(maze[n-2][n-2].num)return;//若出口为0,则跳出inti=n-2,j=n-2;intpow,a=0;do//开始寻找线路{if((!maze[i][j].num)&&(maze[i][j].visited==0))//如果是通路且从未被访问{if((i!=Stack.getTI())||(j!=Stack.getTJ()))//如果该点未入栈,则入栈,访问该点{Stack.push(i,j);maze[i][j].visited=1;//且把访问位置为1}if(i==1&&j==1)//如果是出口则跳出全部循环{a=1;break;}i=i;//初始向左访问j=j-1;pow=0;}else//如果不是通路{intback1,back2;pow++;//方向位改变i=Stack.getTI();//回到访问的点j=Stack.getTJ();while(1){if(pow==1)//向左一位访问{j--;if((i==Stack.getI())&&(j==Stack.getJ()))//如果是来路{pow++;//则改变方向位i=Stack.getTI();//回到访问的点j=Stack.getTJ();continue;}elsebreak;}elseif(pow==2)//向上一位访问{i--;if((i==Stack.getI())&&(j==Stack.getJ())){pow++;i=Stack.getTI();j=Stack.getTJ();continue;}elsebreak;}elseif(pow==3)//向下一位访问{i++;if((i==Stack.getI())&&(j==Stack.getJ())){pow++;i=Stack.getTI();j=Stack.getTJ();continue;}elsebreak;}elseif(pow==4)//向右一位访问{j++;if((i==Stack.getI())&&(j==Stack.getJ())){pow++;i=Stack.getTI();j=Stack.getTJ();continue;}elsebreak;}else{Stack.pop(back1,back2);//若四个方向均不通,就弹栈maze[back1][back2].num=1;//将该点的通路位置为1i=Stack.getTI();//继续往回的点找通路j=Stack.getTJ();pow=0;break;}}}if(Stack.StackEmpty())//如果栈为空,则表示无通路。break;}while(a==0);if(!Stack.StackEmpty()){intx,y;intv=Stack.StackLength()-1;//v用来访问栈inttotal=Stack.StackLength();while(total){Stack.visit(x,y,v);maze[x][y].num=8;total--;}system(pause);system(cls);cout线路为:endl;ofstreamoutfile(迷宫线路.txt,ios::app);for(inti=0;in;i++)for(intj=0;jn;j++){if(maze[i][j].num==8){coutsetw(2)setiosflags(ios::left)×;cout.width(2);outfilesetiosflags(ios::left)×;}else{coutsetw(2)setiosflags(ios::left)maze[i][j].num;cout.width(2);outfilesetiosflags(ios::left)maze[i][j].num;}if(j==n-1){coutendl;outfileendl;}}outfileendl;Stack.output();}else{system(pause);system(cls);Stack.output();//输出通路}Stack.DestroyStack();//销毁栈}voidmenu(){MiGongm;//定义MiGong类inti=0;cout************************欢迎进入迷宫游戏**********************endl;coutendl;cout进入游戏endl;coutendl;cout**************************************************************endl;do{creat();//建立迷宫seek();//寻找通路while(1){cout0.继续游戏。endl;cout1.退出游戏。endl;cini;if(i!=0&&i!=1){cout输入错误,请重新输入!endl;continue;}else{cout游戏结束endl;break;}}}while(!i);}};intmain(){MiGongm;//定义MiGong类m.menu();return0;}