#include<stack>
#include<iostream>
#include<windows.h>
#include<ctime>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int maze[12][12]=
{
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,0,1,1,1,1,0,0,1,0},
{0,1,1,0,1,1,0,1,1,0,1,0},
{0,1,1,1,1,0,1,1,0,0,1,0},
{0,0,0,1,0,0,0,1,1,1,0,0},
{0,0,1,1,1,0,1,0,0,1,0,0},
{0,0,1,0,0,1,1,1,1,0,0,0},
{0,0,1,1,1,1,0,0,1,0,0,0},
{0,0,1,0,0,1,0,0,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0}
};
typedef struct
{
int x;
int y;
}jiegou;
stack<jiegou>z;
void xunlu(int maze[12][12])
{
int mark[12][12]={0};
int g,h;
jiegou a,b;
a.x=1,a.y=1;
z.push(a);
mark[1][1]=1;
while(!(b.x==10&&b.y==10)&&!(z.empty()))
{
b=z.top();
g=b.y;
h=b.x;
if(((maze[g][h+1]==1)||(maze[g][h+1]==5))&&(mark[g][h+1]==0))
{
b.x=h+1;
mark[g][h+1]=1;
for(int i=0;i<=11;i++)
{
for(int j=0;j<=11;j++)
{
if(mark[i][j]==1&&maze[i][j]==1)
{
cout<<"☆";
}
else if(mark[i][j]==1&&maze[i][j]==5)
{
cout<<"# ";
}
else if(mark[i][j]==3)
{
cout<<"* ";
}
else{
if(maze[i][j]==0)
cout<<"●";
if(maze[i][j]==1)
cout<<"○";
}
}
cout<<endl;
}
Sleep(800);
system("cls");
maze[g][h+1]=5;
z.push(b);
}
else if(((maze[g+1][h]==1)||(maze[g+1][h]==5))&&(mark[g+1][h]==0))
{
b.y=g+1;
mark[g+1][h]=1;
for(int i=0;i<=11;i++)
{
for(int j=0;j<=11;j++)
{
if(mark[i][j]==1&&maze[i][j]==1)
{
cout<<"☆";
}
else if(mark[i][j]==1&&maze[i][j]==5)
{
cout<<"# ";
}
else if(mark[i][j]==3)
{
cout<<"* ";
}
else{
if(maze[i][j]==0)
cout<<"●";
if(maze[i][j]==1)
cout<<"○";
}
}
cout<<endl;
}
Sleep(800);
system("cls");
maze[g+1][h]=5;
z.push(b);
}
else if(((maze[g][h-1]==1)||(maze[g][h-1]==5))&&(mark[g][h-1]==0))
{
b.x=h-1;
mark[g][h-1]=1;
for(int i=0;i<=11;i++)
{
for(int j=0;j<=11;j++)
{
if(mark[i][j]==1&&maze[i][j]==1)
{
cout<<"☆";
}
else if(mark[i][j]==1&&maze[i][j]==5)
{
cout<<"# ";
}
else if(mark[i][j]==3)
{
cout<<"* ";
}
else{
if(maze[i][j]==0)
cout<<"●";
if(maze[i][j]==1)
cout<<"○";
}
}
cout<<endl;
}
Sleep(800);
system("cls");
maze[g][h-1]=5;
z.push(b);
}
else if(((maze[g-1][h]==1)||(maze[g-1][h]==5))&&(mark[g-1][h]==0))
{
b.y=g-1;
mark[g-1][h]=1;
for(int i=0;i<=11;i++)
{
for(int j=0;j<=11;j++)
{
if(mark[i][j]==1&&maze[i][j]==1)
{
cout<<"☆";
}
else if(mark[i][j]==1&&maze[i][j]==5)
{
cout<<"# ";
}
else if(mark[i][j]==3)
{
cout<<"* ";
}
else{
if(maze[i][j]==0)
cout<<"●";
if(maze[i][j]==1)
cout<<"○";
}
}
cout<<endl;
}
Sleep(800);
system("cls");
maze[g-1][h]=5;
z.push(b);
}
else if(((maze[g][h+1]==1)||(maze[g][h+1]==5))&&(mark[g][h+1]==1))
{
b.x=h+1;
mark[g][h+1]=3;
for(int i=0;i<=11;i++)
{
for(int j=0;j<=11;j++)
{
if(mark[i][j]==1&&maze[i][j]==1)
{
cout<<"☆";
}
else if(mark[i][j]==1&&maze[i][j]==5)
{
cout<<"# ";
}
else if(mark[i][j]==3)
{
cout<<"* ";
}
else{
if(maze[i][j]==0)
cout<<"●";
if(maze[i][j]==1)
cout<<"○";
}
}
cout<<endl;
}
Sleep(800);
system("cls");
maze[g][h+1]=5;
z.push(b);
}
else if(((maze[g+1][h]==1)||(maze[g+1][h]==5))&&(mark[g+1][h]==1))
{
b.y=g+1;
mark[g+1][h]=3;
for(int i=0;i<=11;i++)
{
for(int j=0;j<=11;j++)
{
if(mark[i][j]==1&&maze[i][j]==1)
{
cout<<"☆";
}
else if(mark[i][j]==1&&maze[i][j]==5)
{
cout<<"# ";
}
else if(mark[i][j]==3)
{
cout<<"* ";
}
else{
if(maze[i][j]==0)
cout<<"●";
if(maze[i][j]==1)
cout<<"○";
}
}
cout<<endl;
}
Sleep(800);
system("cls");
maze[g+1][h]=5;
z.push(b);
}
else if(((maze[g][h-1]==1)||(maze[g][h-1]==5))&&(mark[g][h-1]==1))
{
b.x=h-1;
mark[g][h-1]=3;
for(int i=0;i<=11;i++)
{
for(int j=0;j<=11;j++)
{
if(mark[i][j]==1&&maze[i][j]==1)
{
cout<<"☆";
}
else if(mark[i][j]==1&&maze[i][j]==5)
{
cout<<"# ";
}
else if(mark[i][j]==3)
{
cout<<"* ";
}
else{
if(maze[i][j]==0)
cout<<"●";
if(maze[i][j]==1)
cout<<"○";
}
}
cout<<endl;
}
Sleep(800);
system("cls");
maze[g][h-1]=5;
z.push(b);
}
else if(((maze[g-1][h]==1)||(maze[g-1][h]==5))&&(mark[g-1][h]==1))
{
b.y=g-1;
mark[g-1][h]=3;
for(int i=0;i<=11;i++)
{
for(int j=0;j<=11;j++)
{
if(mark[i][j]==1&&maze[i][j]==1)
{
cout<<"☆";
}
else if(mark[i][j]==1&&maze[i][j]==5)
{
cout<<"# ";
}
else if(mark[i][j]==3)
{
cout<<"* ";
}
else{
if(maze[i][j]==0)
cout<<"●";
if(maze[i][j]==1)
cout<<"○";
}
}
cout<<endl;
}
Sleep(800);
system("cls");
maze[g-1][h]=5;
z.push(b);
}
else z.pop();
}
if(!z.empty())
{
cout<<"所走的路径为:" <<endl;
while(!(z.empty()))
{
b=z.top();
cout<<"("<<b.y<<","<<b.x<<")"<<" ";
z.pop();
}
}
else
cout<<"无路径可走!"<<endl;
}
int main()
{
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"< 欢迎来到迷宫程序之家 ! >"<<endl;
cout<<" "<<endl;
Sleep(1000);
system("cls");
mciSendString("play ll.wav",NULL,0,NULL);
int f,r;
cout<<"符号说明:○( 此符号为路 )"<<endl;
cout<<"符号说明:●( 此符号为墙 )"<<endl;
cout<<"符号说明:☆(此符号为寻路图标 )"<<endl;
cout<<"符号说明:# (此符号为走过一次的路 )"<<endl;
cout<<"符号说明:* (此符号为重复走过的路 )"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"< 请选择您的模式!(输入情况 1 或者情况 2 ) >" <<endl;
cout<<" "<<endl;
cout<<"< 情况1:默认迷宫(可以显示路径返回过程) >"<<endl;
cout<<" "<<endl;
cout<<"< 情况2:随机迷宫(大概率不会遇到死路) >" <<endl;
cout<<" "<<endl;
cout<<"< 请收入您的选择( 1 or 2 ): "<<endl;
cout<<" ";
cin>>f;
if(f==1)
{
xunlu(maze);
}
if(f==2)
{
for(int i=0;i<=11;i++)//生成一个随机的迷宫
{
for(int j=0;j<=11;j++)
{
r=rand()%10;
if(r<=1)
maze[i][j]=0;
else
maze[i][j]=1;
if(i==0||j==0||i==11||j==11)
maze[i][j]=0;
maze[10][10]=1;
maze[1][1]=1;
}
}
xunlu(maze);
}
return 0;
}