网站大量收购独家精品文档,联系QQ:2885784924

可连通迷宫程序.doc

  1. 1、本文档共10页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
可连通迷宫程序

#include stdafx.h #include iostream #include stdlib.h #include stdio.h #include time.h #include math.h #include Windows.h #include queue #define M 20 #define N 20 using namespace std;//曝光命名空间 typedef struct{ int r;//通道块的行坐标row int c;//通道块的列坐标col int di;//走向下一通道块的方向direction int ord;//当前路径的序列号order }PosType,Stack;//栈记录通道块的各元素 int dir[4][2] = { -1, 0, 1, 0, 0, -1, 0, 1 }; //临近的四个方向,表示和某点周围的横纵坐标差值 void InitStartEnd(PosType* start,PosType* end, int degree){ start-r=rand()%(M-3)+1;//随机入口从1---(M-3) start-c=0; start-di=1; start-ord=0; //随机入口 end-r=rand()%(M-3)+1;//随机出口从1---(M-3) end-c=N-1; //随机出口 if ( degree 1 abs(end-r - start-r) = M/2 M 8) InitStartEnd( start, end, degree );//当难度为2、3、4时,为了增加难度,如果产生的起点和终点在同一行,则重新产生。 } void CreateWell( char Maze[M][N], PosType start, PosType end, int degree ) { int i, j; Maze[start.r][start.c]=s;//设置起点 Maze[end.r][end.c]=e;//设置终点 start.c ++; end.c --; queue PosType Q; Q.push( start ); Maze[start.r][start.c] = ; bool flag[4]; PosType temp, temp1, father[M][N]; bool map[M][N]; memset( map, true, sizeof(map) ); while ( !Q.empty() ) { temp = Q.front(); Q.pop(); if ( temp.c == end.c temp.r == end.r ) break; int t = 0; memset( flag, true, sizeof(flag) ); while ( t != 4 ) { i = rand() % 4; if ( !flag[i] ) continue; t++; flag[i] = false; temp1 = temp; temp1.r += dir[i][0]; temp1.c += dir[i][1]; if ( temp1.r = 0 || temp1.r = M-1 || temp1.c = 0 || temp1.c = N-1 ) continue; if ( map[temp1.r][temp1.c] == false ) continue; map[temp1.r][temp1.c] = false; Q.push( temp1 ); father[temp1.r][temp1.c].c = temp.c; father[temp1.r][temp1.c].r = temp.r; } } int x, y; //打通通路 while ( temp.c != start.c || temp.r != start.r ) { Maze[temp.r][temp.c] = ; x = temp.r; y = temp.c; temp.r = father[x][y].r; temp.c = father[x][y].c; } /////根据难度设置不同的通路与墙的比例////// int u=1; switch ( degree ) { case 1: u = N*M/1; break; case 2: u = N*M/2; break; case 3: u = N*M/4;

文档评论(0)

jgx3536 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:6111134150000003

1亿VIP精品文档

相关文档