#include <iostream>
#include <ctime>
#include <vector>
using namespace std ;
typedef unsigned int UINT ;
namespace Maze
{
void SetMaze (UINT Dimension , UINT Level);
UINT SetLevel (UINT Dimension ,UINT Level);
UINT GetRand (UINT tRand); // 获取随机数
};
int main ( void )
{
cout<<"Maze Game : "<<endl;
using namespace Maze ;
UINT TDimension = 0,
Tlevel = 0;
char play;
do
{
cout<<"Input The Dimension (3-18) : "<<endl; // 设置维数最小为3,最大为18
cin >> TDimension;
while(TDimension < 3 || TDimension > 18)
{
cout<<"The Dimension is Wrong ! Please input again. (3-18)"<<endl;
cin >> TDimension;
}
cout<<"Input The Level (1-8)"<<endl; //设置级别最小为1,最大为8
cin >> Tlevel;
while(Tlevel < 1 || Tlevel > 8)
{
cout<<"The Level is Wrong ! Please input again.(1-8)"<<endl;
cin >> Tlevel;
}
SetMaze(TDimension,Tlevel);
cout<<"Play Again (Y or N)?"<<endl;
cin>> play ;
} while(play == 'Y'|| play == 'y');
return 0 ;
}
namespace Maze
{
UINT SetLevel (UINT Dimension ,UINT Level)
{
// 除入口和出口,所设置 1 的个数
return (UINT)(( 1 - Level*0.1) * Dimension * Dimension - 2);
}
UINT GetRand(UINT tRand)
{
return (UINT)(rand() % tRand); // 获取随机数
}
void SetMaze(UINT Dimension , UINT Level)
{
// 设置一个二维向量
vector<vector<int> > Nnum(Dimension, vector<int>(Dimension));
UINT i = 0 ;
UINT j = 0;
for ( i = 0; i < Dimension; i++)
{
for ( j = 0; j < Dimension; j++)
{
// 入口和出口元素设置为1
if ( (i == 0 && j==0) || ( i == Dimension - 1 && j == Dimension - 1) )
{
Nnum[i][j] = 1;
}
else
Nnum[i][j] = 0; // 其它全部设置成 0
}
}
srand( (unsigned)time( NULL ) );
UINT count = 0; // 用来记录元素被置为1 的个数
while (count!=SetLevel (Dimension , Level) )
{
UINT s = GetRand(Dimension); // 随机获取行坐标
UINT t = GetRand(Dimension); // 随机获取列坐标
if (Nnum[s][t] != 1)
{
Nnum[s][t] = 1 ; // 把随机获取的行,列所对应的元素的值 置为 1
count++;
}
}
for ( i = 0 ; i < Dimension+2; i++)
{
for ( j = 0 ; j < Dimension+2; j++)
{
if (i == 0 || i == Dimension+1 )
{
cout<<"*"<<" "; // 在迷宫外围以 * 打印围墙
}
else if (j == 0 || j == Dimension+1 )
{
cout<<"*"<<" ";
}
else if ( i > 0 && j > 0 )
cout << Nnum[i-1][j-1] <<" ";
}
cout<<endl;
}
// 用二维数组来定义八个方向
int Way[8][2] = {{1,1},{1,0},{1,-1},{0,1},{-1,0},{-1,1},{0,-1},{-1,-1}};
vector< int > tempX; // 用来保存元素的行数
vector< int > tempY; // 用来保存元素的列数
UINT m = 0,
n = 0,
Tcount = 0;
UINT NnumX = 0 ,
NnumY = 0;
// 首先把入口元素的坐标放入向量
tempX.push_back( m );
tempY.push_back( n ) ;
Nnum[0][0] = 2 ; // 入口元素的值被置为 2
while( !tempX.empty() && ( m != Dimension-1 || n != Dimension-1 ))
{
i = 0;
while ( i < 8 ) // 进行八个方向查找
{
m = Way[i][0] + NnumX ;
n = Way[i][1] + NnumY ;
if (m >= 0 && m < Dimension)
{
if (n >= 0 && n < Dimension)
{
if( Nnum[m][n] == 1 ) // 找到一条通路
{
// 将其坐标值分别放入向量
tempX.push_back( m );
tempY.push_back( n );
Nnum[m][n] = 2; // 其值被置为 2 ,记录已经走过
NnumX = m ;
NnumY = n ;
Tcount ++;
break ;
}
}
}
i++ ;
}
if (i == 8 ) // 找不到一条通路
{
// 将向量末尾的坐标 删除
tempX.pop_back();
tempY.pop_back();
Tcount --;
}
// 返回到上一个坐标点
if(Tcount >= 0 )
{
NnumX = tempX[Tcount];
NnumY = tempY[Tcount];
}
}
if (tempX.empty()) // 向量为空,即为未找到迷宫通路
{
cout<<"Cannot find a Way!!!"<<endl;
}
else
{ //打印通路时每步的坐标
cout <<"The Path Is : " <<endl;
for ( i = 0; i <= Tcount; i++ )
{
cout <<i+1<< ": (" << tempX[i] << "," << tempY[i] << ")" << "\t";
}
}
cout << endl;
}
}