扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章主要介绍C++自动生成迷宫游戏的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
在丰台等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、做网站、成都外贸网站建设公司 网站设计制作按需开发,公司网站建设,企业网站建设,品牌网站设计,成都全网营销推广,外贸营销网站建设,丰台网站建设费用合理。运用并查集自动生成迷宫地图,并运用队列和栈寻找迷宫通路并打印出来
#include#include #include #include #include using namespace std; using std::queue; using std::stack; typedef struct Point { int x; int y; int d;//方向 若方向为-1,则表示起点 }Point; queue mqueue; stack mstack; Point pos, pos1; int m, n;//迷宫行(tm-1)/2和列(tn-1)/2 int tm, tn;//实际作图 int x, y, tx1, tx2, ty1, ty2;//点坐标 int d; int s[10000000]; int maze[1000][1000], mark[1000][1000];//大迷宫 int sign[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };//上下左右四个方向 0上 1下 2上 3下 Point start; int Find_x(int x); void unionSets(int node1, int node2); void Init(); int getAdd(int x, int y); void foundpath(); void fixmaze(); int connected(int node1, int node2); void Findpath(); void changemaze(); int main() { Init(); cout << "请输入迷宫规模2x-1,2y-1:(x y)" << endl; cin >> m >> n; tm = m * 2 + 1; tn = n * 2 + 1; start.x = 1; start.y = 1; start.d = -1; mqueue.push(start); for (int i = 0; i < tm; i++) { for (int j = 0; j < tn; j++) { maze[i][j] = 1; mark[i][j] = 0; } } for (int i = 1; i < tm - 1; i += 2) { for (int j = 1; j < tn - 1; j += 2) maze[i][j] = 0; } srand(time(NULL)); foundpath(); fixmaze(); cout << "迷宫全图:" << endl; for (int i = 0; i < tm; i++) { for (int j = 0; j < tn; j++) { if (maze[i][j] == 1) cout << "▇"; else if (maze[i][j] == 0) cout << "□"; } cout << endl; } Findpath(); changemaze(); cout << "找到的通路:“..”表示:" << endl; for (int i = 0; i < tm; i++) { for (int j = 0; j < tn; j++) { if (maze[i][j] == 1) cout << "▇"; else if (maze[i][j] == 0) cout << "□"; else if (maze[i][j] == -1) cout << ".."; } cout << endl; } system("pause"); return 0; } int connected(int node1, int node2) { return Find_x(node1) == Find_x(node2); } int Find_x(int x) { if (s[x] < 0) return x; else return Find_x(s[x]); }; void unionSets(int node1, int node2) { int root1 = Find_x(node1); int root2 = Find_x(node2); if (root1 == root2) return; if (s[root2] < s[root1]) s[root1] = root2; else { if (s[root1] == s[root2]) s[root1]--; s[root2] = root1; } }; int getAdd(int x, int y) { return (x*tn + y); }; void Init() { for (int i = 0; i < 10000000; ++i) s[i] = -1; }; void foundpath() { while (connected(getAdd(1, 1), getAdd(tm - 2, tn - 2)) != 1) { do { x = rand() % (tm - 2) + 1; y = rand() % (tn - 2) + 1; } while (maze[x][y] == 0); d = x % 2; if (d == 0) { tx1 = x + 1; ty1 = y; tx2 = x - 1; ty2 = y; if (connected(getAdd(tx1, ty1), getAdd(tx2, ty2)) != 1) { maze[x][y] = 0; unionSets(Find_x(getAdd(tx1, ty1)), Find_x(getAdd(tx2, ty2))); } } else if (d == 1) { tx1 = x; ty1 = y + 1; tx2 = x; ty2 = y - 1; if (connected(getAdd(tx1, ty1), getAdd(tx2, ty2)) != 1) { maze[x][y] = 0; unionSets(Find_x(getAdd(tx1, ty1)), Find_x(getAdd(tx2, ty2))); } } } } void fixmaze() { for (int i = 1; i < tm - 1; i++) { for (int j = 1; j < tn - 1; j++) { if (maze[i - 1][j] == 1 && maze[i + 1][j] == 1 && maze[i][j + 1] == 1 && maze[i][j - 1] == 1) { maze[i][j] = 1; } } } for (int i = 1; i < tm - 1; i++) { for (int j = 1; j < tn - 1; j++) { if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 1 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 1 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 1 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 1 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 1 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 1 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 1 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 1 && maze[i + 1][j + 1] == 0) { maze[i][j] = 1; } if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 1) { maze[i][j] = 1; } } }//局部优化,防止出现大面积通路 } void Findpath() { int flag = 0; int i, j; while (!mqueue.empty()) { i = mqueue.front().x; j = mqueue.front().y; mark[i][j] = 1; for (int k = 0; k < 4; k++) { if (mark[i + sign[k][0]][j + sign[k][1]] == 0 && maze[i + sign[k][0]][j + sign[k][1]] == 0) { pos.x = i + sign[k][0]; pos.y = j + sign[k][1]; pos.d = k; mark[pos.x][pos.y] = 1; mqueue.push(pos); if (mqueue.back().x == tm - 2 && mqueue.back().y == tn - 2) { mstack.push(mqueue.front()); mstack.push(mqueue.back()); flag = 1; break; } } } if (flag) break; mstack.push(mqueue.front()); if (!mqueue.empty()) mqueue.pop(); } } void changemaze() { int i, j, k; i = mstack.top().x; j = mstack.top().y; k = mstack.top().d; maze[i][j] = -1; while (mstack.size()>0) { if (mstack.top().x == i - sign[k][0] && mstack.top().y == j - sign[k][1]) { i = i - sign[k][0]; j = j - sign[k][1]; k = mstack.top().d; maze[i][j] = -1; if (!mstack.empty()) mstack.pop(); } else if (!mstack.empty()) mstack.pop(); } }
以上是“C++自动生成迷宫游戏的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联网站建设公司行业资讯频道!
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流