迷宫实现
//迷宫
#include<iostream>
#include<ctime>
#include<windows.h>
#include<fstream>
using namespace std;
struct offsets
{
int a,b;
char *dir;
};
struct node
{
int x,y;
};
struct tag
{
int x,y;
char *dir;
};
class MiGong
{
private:
int map[30][30];//地图矩阵
int mark[30][30];//标志矩阵
offsets move[4];//各个方向的偏移量
node export;//出口
int row[30];//记录通路每个结点的行号
tag path[500];//通路的路径
int n;//矩阵的阶数
int length;//整个路径的结点数
public:
ofstream outfile;
MiGong();
void InitMap();
static void setcolor(int color);
void PrintMap(int g,int h);
int Seekrow(node temp);
void gotoxy(int x,int y);
void change(int x,int y);
void displayRoute();
};
MiGong::MiGong()
{
length=0;
int i,j;
cout<<"请输入矩阵的阶数:"<<endl;
cin>>n;
n--;
export.x=n-1;export.y=n;//出口
move[1].a=0;move[1].b=1;move[1].dir="E";
move[2].a=-1;move[2].b=0;move[2].dir="N";
move[0].a=1;move[0].b=0;move[0].dir="S";
move[3].a=0;move[3].b=-1;move[3].dir="W";
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
mark[i][j]=0;
}
void MiGong::setcolor(int color)
{
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hout,color);
}
void MiGong::gotoxy(int x,int y)
{
COORD pos={x,y};
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}//屏幕从左向右代表x的正方向,从上至下为y正方向。
void MiGong::InitMap()
{
int i,j;
for(i=0;i<=n;i++)
{
map[0][i]=map[n][i]=map[i][0]=map[i][n]=1;
}
map[1][0]=map[n-1][n]=0;
srand((unsigned)time(NULL)+rand());//随机生成器的随机种子
for(i=1;i<=n-1;i++)//随机生成地图
{
for(j=1;j<=n-1;j++)
{
map[i][j]=rand()%2;
if(i==n/4||i==n/2||i==(n-n/3)||i==n-1||j==1||i==n-5)
map[i][j]=0;
}
}
for(i=1;i<=3;i++)
{
j=5+rand()%(n+1-5);
map[j][1]=1;
j=1+rand()%(n-n/4);
map[n-1][j/2]=1;
map[n-n/3][rand()%n]=1;
}
}
void MiGong::PrintMap(int g,int h)
{
int i,j;
system("cls");
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(i==1&&j==0)
{
setcolor(11);
cout<<" →";
}
else if(j==0)
cout<<" ";
if(map[i][j]==1)//1表示墙
{
setcolor(12);
cout<<"□";
}
else if(i==g&&h==j)
{
setcolor(10);
cout<<"☆";
}
else if(map[i][j]==0)
cout<<" ";
}
if(i==n-1)
{
setcolor(11);
cout<<"→";
}
cout<<endl;
}
Sleep(50);
}
void MiGong::change(int x,int y)
{
int j;
for(j=0;j<=n;j++)
{
if(x==1&&j==0)
{
setcolor(11);
cout<<" →";
}
else if(j==0)
cout<<" ";
if(map[x][j]==1)//1表示墙
{
setcolor(12);
cout<<"□";
}
else if(y==j)
{
setcolor(10);
cout<<"☆";
}
else if(map[x][j]==0)
cout<<" ";
}
if(x==n-1)
{
setcolor(11);
cout<<"→";
}
cout<<endl;
Sleep(100);
}
int MiGong::Seekrow(node temp)
{
int i,g,h;
char *d;//g,h记录位置信息,d记录方向
if(temp.x==export.x&&temp.y==export.y)
{
return 1;
}
else
{//以到达出口,函数返回1
node tp;
for(i=0;i<4;i++)
{
g=tp.x=temp.x+move[i].a;
h=tp.y=temp.y+move[i].b;
d=move[i].dir;
if(map[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
if(Seekrow(tp))
{
path[length].x=g;path[length].y=h;path[length].dir=d;
length++;
return 1;
}
}
}
}
if(temp.x==1&&temp.y==0)
{
gotoxy(0,1);
change(1,0);
gotoxy(0,n+1);
setcolor(8);
cout<<"\n随机生成的迷宫没通路......没通路......"<<endl;
setcolor(11);
cout<<"你的人品不好哦......"<<endl;
}
return 0;
}
void MiGong::displayRoute()
{
path[length].x=1;
path[length].y=0;
path[length].dir="E";
length++;
int j;
for(j=length-2;j>0;j--)
{
gotoxy(0,path[j].x);
change(path[j].x,path[j].y);
gotoxy(0,path[j].x);
change(path[j].x,2*n);
}
gotoxy(0,n-1);
change(n-1,n);
gotoxy(0,n+1);
cout<<"路径长度:"<<length-1<<endl<<endl;
cout<<"路径:"<<endl;
for(j=length-1;j>0;j--)
cout<<"("<<path[j].x<<","<<path[j].y<<","<<path[j].dir<<")→";
cout<<"("<<path[0].x<<","<<path[0].y<<","<<path[0].dir<<")"<<endl<<endl;
}
void main()
{
MiGong path;
path.InitMap();
path.PrintMap(30,30);
node entry;
entry.x=1;
entry.y=0;
if(path.Seekrow(entry))
path.displayRoute();
system("pause");
}