迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx

上传人:b****2 文档编号:15118199 上传时间:2022-10-27 格式:DOCX 页数:15 大小:18.62KB
下载 相关 举报
迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx_第1页
第1页 / 共15页
迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx_第2页
第2页 / 共15页
迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx_第3页
第3页 / 共15页
迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx_第4页
第4页 / 共15页
迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx

《迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。

迷宫生成及寻路右手贴墙 C++实现Word格式文档下载.docx

//0上,1下,2左,3右

isVisit=false;

}

private:

boolisVisit;

//节点是否被访问

intx,y;

//横纵坐标

intwall[4];

//四堵墙

};

classPoint

intdirection;

//走的方向

//横纵坐标

intx;

inty;

classmaze

maze(intm=11,intn=11);

voidgenerator();

//迷宫生成器

voidprintMaze();

//打印迷宫

booljudgeCell(int,int);

//判断当前节点的某个相临节点是否能成为下个当前节点

voidbreakWall(MazeCell*);

//拆掉两个节点之间墙的函数

voidtravel();

//迷宫寻路函数

~maze();

//必须要有析构函数清除动态申请内存,防止内存泄露

intstartPoint;

//迷宫入口格子开始的横坐标

intendPoint;

//迷宫出口格子的纵坐标

introw;

//节点矩阵行数

intcolumn;

//节点矩阵列数

stack<

MazeCell>

mazeStack;

//存放节点的堆栈

MazeCell**matrix;

//生成迷宫的节点矩阵

inttotal;

//节点的总个数

intvisit;

//被访问过的节点个数

int**graph;

//迷宫图

intgraphRow;

//迷宫的行数

intgraphColumn;

//迷宫的列数

MazeCell*currentCell;

//记录深度优先遍历节点时正在访问的节点

PointgoPoint;

//记录迷宫寻路时当前所在位置(格子)的信息

//定义走迷宫时的方向,分别为上下左右

constintshang=0;

constintxia=1;

constintzuo=2;

constintyou=3;

maze:

:

maze(intm,intn)

while(m%2==0||n%2==0)

cout<

<

"

\n输入错误!

请重新输入!

;

cin>

>

m>

n;

visit=0;

//访问过的节点数应当初始化为0

//注意最后生成的迷宫与所构造的节点矩阵应当有这样的关系,rowcolumn指的是节点矩阵的行列

row=(m-1)/2;

column=(n-1)/2;

total=row*column;

//所有的节点数

srand(time(0));

//初始化节点矩阵

matrix=newMazeCell*[row];

for(inti=0;

row;

matrix[i]=newMazeCell[column];

for(intj=0;

j<

column;

j++)

{

matrix[i][j].x=i;

matrix[i][j].y=j;

}

//注意矩阵节点跟迷宫格子的关系

graphRow=m;

graphColumn=n;

//初始化迷宫

graph=newint*[graphRow];

graphRow;

graph[i]=newint[graphColumn];

graphColumn;

graph[i][j]=1;

graph[2*i+1][2*j+1]=0;

//把迷宫中除了代表(矩阵节点及节点相邻的四堵墙)的其他格子弄为墙

}

~maze()

delete[]matrix[i];

delete[]matrix;

delete[]graph[i];

delete[]graph;

voidmaze:

printMaze()

if(graph[i][j]==0)//没有走过的格子

cout<

'

'

elseif(graph[i][j]==1)//墙

#'

elseif(graph[i][j]==2)

o'

//第一次走过的格子

else

x'

//第二次走过的格子

endl;

breakWall(MazeCell*tempCell)

intrelation;

//存放当前节点与上一个被访问节点的位置关系,下面以此为依据进行拆墙

if(tempCell->

x!

=currentCell->

x)//关系为上下相邻

relation=tempCell->

x-currentCell->

x;

if(relation==1)

currentCell->

wall[1]=tempCell->

wall[0]=0;

else

wall[0]=tempCell->

wall[1]=0;

else//左右相邻

y-currentCell->

y;

currentCell->

wall[3]=tempCell->

wall[2]=0;

wall[2]=tempCell->

wall[3]=0;

boolmaze:

judgeCell(intx,inty)//判断当前节点的某个相临节点是否能成为下个当前节点

//是否有效,木有越界

if(x<

0||x>

row-1)

returnfalse;

if(y<

0||y>

column-1)

//是否被访问过

if(matrix[x][y].isVisit)

returntrue;

generator()

intcount1=0,count2=0;

//count1为记录前一个被访问节点(堆栈顶部节点)有效相邻节点数,count2为当前访问节点的有效相邻节点数

currentCell=&

matrix[0][0];

//从0,0开始访问,也可以随机取一个节点

currentCell->

isVisit=true;

//初始点为0,0,并标记

visit+=1;

intcurrentX=currentCell->

intcurrentY=currentCell->

MazeCell*tempCell;

//存放选好的下一个要访问的节点

while(total>

visit)//当还有点没被访问时

inti=0;

//记录有效相邻节点数

MazeCell*temp[4];

//存放有效相邻节点的数组

if(judgeCell(currentX+1,currentY))

temp[i]=&

matrix[currentX+1][currentY];

i++;

if(judgeCell(currentX-1,currentY))

matrix[currentX-1][currentY];

if(judgeCell(currentX,currentY+1))

matrix[currentX][currentY+1];

if(judgeCell(currentX,currentY-1))

matrix[currentX][currentY-1];

if(visit==0)

count2=i;

inttempp;

tempp=count2;

count1=tempp;

if(i>

0)

ints=rand()%i;

//从有效相邻节点中随机选取一个出来作为下一个要访问的节点

tempCell=temp[s];

breakWall(tempCell);

//拆墙

mazeStack.push(*currentCell);

//将上一个节点进栈

currentCell=tempCell;

visit++;

currentX=currentCell->

currentY=currentCell->

if(!

mazeStack.empty()&

&

count1>

1)//当前节点无有效的相邻节点,并且前一个节点并非仅仅只有当前节点一个有效相邻节点(避免死循环)原路返回,返回到上一个节点

{

currentCell->

isVisit=false;

visit--;

currentCell=&

mazeStack.top();

mazeStack.pop();

}

else//如若上一个被访问节点只有当前节点一个有效相邻节点,则从所有被访问过的节点中随机选取一个作为当前节点(从路径展开分支,保证路径不会断)

mazeStack.push(*currentCell);

inta1=rand()%row,a2=rand()%column;

while(!

matrix[a1][a2].isVisit)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1