C++随机迷宫生成[转载]
原文:http://tieba.baidu.com/p/2596809144
#include<iostream.h>
#include"time.h"
#include"stdlib.h" const char road = ' ';
const char wall = 'w';
const char connect = ;
const char began = ;
const char end = ;
const char path = '.';
const char up = ;
const char down = ;
const char right = ;
const char left = ;
const char walked =wall; void Outmaze(int size,char **block);
void Modify(int size,char ** block);
void Join(int size,char ** block); char ** InitializeMaze(int size)
{//this function used to create a random maze //create a 2D array to store the maze
char **block=new char*[size] ;
for(int n=;n<size;n++)
{
block[n]=new char[size];
} //initialize the 2D array
for(int i=;i<size;i++)
{
for(int j=;j<size;j++)
{
if(i%==&&j%==)
{
block[i][j]=road;
}
if(i%!=&&j%!=)
{
block[i][j]=wall;
}
else
{
if(i%!=||j%!=)
{
if(rand()%<)
{
block[i][j]=road;
}
else
{
block[i][j]=wall;
}
}
}
}
} //but the 2D array is not a maze , we should modify it
Modify(size,block); return block;
} void Outmaze(int size,char **block)
{//output the maze //output the top bound
for(int m=;m<size*+;m++)
cout<<connect;
cout<<endl; for(int i=;i<size;i++)
{ for(int j=;j<size;j++)
{
if(j==)
{//output the left bound
cout<<connect<<' ';
} cout<<block[i][j]<<' '; if(j==size-)
{//output the right bound
cout<<connect;
}
}
cout<<endl;
} //output the bottom bound
for(int n=;n<size*+;n++)
cout<<connect;
cout<<endl;
} bool TestConnect(int size,int x,int y,char ** block)
{//test wether exists the problem of close int n=; if((x-<)||(block[x-][y]==connect)){n++;} if((x+>size-)||(block[x+][y]==connect)){n++;} if((y-<)||(block[x][y-]==connect)){n++;} if((y+>size-)||(block[x][y+]==connect)){n++;} if(n>=)
{
return true;
}
else
{
return false;
}
} int TagConnect(int size,int x,int y,char ** block)
{//tag the walls that connected to the bound as "connect" //tag the walls and solve problem of close
if((block[x][y]==wall)&&(!TestConnect(size,x,y,block)))
{//if the block is wall and is not close then tag it as "connect"
block[x][y]=connect;
}
else
{//if the block cause close then tag it as "road"
block[x][y]=road;
return ;
} //go along four directs to continue this work
if((x->=)&&block[x-][y]==wall)
TagConnect(size,x-,y,block);//go up
if((x+<=size-)&&block[x+][y]==wall)
TagConnect(size,x+,y,block);//go down
if((y->=)&&block[x][y-]==wall)
TagConnect(size,x,y-,block);//go left
if((y+<=size-)&&block[x][y+]==wall)
TagConnect(size,x,y+,block);//go right
return ;
} void Modify(int size,char ** block)
{//modify the array to be a maze //tag the walls that connected to the bound
for(int i=;i<size-;i=i+)
{
TagConnect(size,i,size-,block);//from the right bound
TagConnect(size,i,,block);//from the left bound
TagConnect(size,size-,i,block);//from the bottom bound
TagConnect(size,,i,block);//from the top bound
} //there still some walls which are isolation (not connect to any bounds),we have to solve the problem
Join(size,block);
} void Join(int size,char ** block)
{//connect the walls that are isolation to the bound for(int i=;i<size-;i++)
{
for(int j=;j<size-;j++)
{
if(block[i][j]==road&&!(i%==&&j%==)&&!(i%!=&&j%!=))
{ if(!TestConnect(size,i,j,block))
{
block[i][j]=wall;
TagConnect(size,i,j,block);
}
}
}
}
} bool FindPath(int size,int x,int y ,int o,int p,char ** block)
{//find the path of the maze. x,y are the coordinate of the began point
// and o,p are the coordinate of the end point if((x==o)&&(y==p))
{
block[x][y]=;
return true;
} block[x][y]=walked; if((x->=)&&block[x-][y]==road)
{
if(FindPath(size,x-,y,o,p,block))
{
block[x][y]=up;
return true;
}
} if((x+<=size-)&&block[x+][y]==road)
{
if(FindPath(size,x+,y,o,p,block))
{
block[x][y]=down;
return true;
}
} if((y->=)&&block[x][y-]==road)
{
if(FindPath(size,x,y-,o,p,block))
{
block[x][y]=left;
return true;
}
} if((y+<=size-)&&block[x][y+]==road)
{
if(FindPath(size,x,y+,o,p,block))
{
block[x][y]=right;
return true;
}
} block[x][y]=road; return false;
} main()
{
do
{
cout<<"welcome !"<<endl;
srand((unsigned int) time());
cout<<"please input the size of the maze:"<<endl;
int size;
cin>>size;
size=size/*+;
char ** block=InitializeMaze(size);
block[][]=began;
block[size-][size-]=end;
Outmaze(size,block); cout<<"press any key to show answer!"<<endl;
char n;
cin>>n;
block[size-][size-]=road;
FindPath(size,,,size-,size-,block);
Outmaze(size,block); }while(true);
return ;
}

路径:

C++随机迷宫生成[转载]的更多相关文章
- 利用纯JS和HTML Canvas生成随机迷宫过程中产生的有趣的事情
上效果图: #先看生成随机迷宫的代码吧↓ <html> <head> <title>生成随机迷宫v1.0</title> </head> & ...
- [迷宫中的算法实践]迷宫生成算法——递归分割算法
Recursive division method Mazes can be created with recursive division, an algorithm which wo ...
- 随机Prim法创建随机迷宫(C#实现)
因为这两天想参加一个比赛,所以就在上网找素材,刚好看到了迷宫生成,就决定拿这个开刀了. 参考的原文地址为(来源页面) 源地址中是使用AS实现的,没学过AS,所以直接不会运行,于是就自己根据原文的概念进 ...
- 一个比较全面的java随机数据生成工具包
最近,由于一个项目的原因需要使用一些随机数据做测试,于是写了一个随机数据生成工具,ExtraRanom.可以看成是Java官方Random类的扩展,主要用于主要用于测试程序.生成密码.设计抽奖程序等情 ...
- 利用Java随机,生成随机学生数据
为模拟向数据库中大量插入学生数据(注:此处应该用PreparedStatement.batchUpdate等批处理提高效率)的情形,通过Java随机来生成学生数据. 一.要生成的学生数据 studen ...
- Clojure——学习迷宫生成
背景 初学clojure,想着看一些算法来熟悉clojure语法及相关算法实现. 找到一个各种语言生成迷宫的网站:http://rosettacode.org/wiki/Maze_generation ...
- 随机数据生成与对拍【c++版,良心讲解】
10.7更新:见最下面 离NOIP2018没剩多长时间了,我突然发现我连对拍还不会,于是赶紧到网上找资料,找了半天发现了一个特别妙的程序,用c++写的! 不过先讲讲随机数据生成吧. 很简单,就是写一个 ...
- solr的随机排序 【转载】
原文地址:http://blog.csdn.net/duck_genuine/article/details/8477336 有这样的一种需求,想从索引库里随机取出4条记录. 在 schema.xml ...
- linux shell 随机字符生成单词
#!/bin/sh #生成随机5个单词 filecount= wordcount= flag= #-lt -le -gt -ge -eq #while [ $f -lt $filecount ]; # ...
随机推荐
- webpack随笔2--编译ES6/ES7
一.Babel 1.安装babel Bable-loader: babeljs.io babel最新版:npm install babel-loader@8.0.0-beta.0 @babel/cor ...
- 【剑指Offer】面试题12. 矩阵中的路径
题目 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左.右.上.下移动一格.如果一条路径经过了矩阵的某一格,那么该路径 ...
- 【Android】家庭记账本手机版开发报告七
一.说在前面 昨天 实现了账单的图标显示 今天 本地化,测试APP,将工程源码放到github上 源码:https://github.com/xiaotian12-call/Android_Boo ...
- 使用模拟器调试react-native步骤(安卓机)
1.在cmd界面搭建react-native 环境: 可参考https://reactnative.cn/docs/0.51/getting-started.html#content (1)npm i ...
- java向python ,text文件动态传参或传值问题完美解决
由于业务需要对python文件进行参数传递,通过下面两个java方法完美解决此问题,我的思路是:首先我要先把上次写的参数删除,第二我要新的参数写到python文件中. 第一个方法解决了删除上次传递的参 ...
- cf 730J. Bottles
搞一个背包,233 要求用的瓶数最少,那么就业瓶数为第一关键,当瓶数相当后再以a[i] #include<bits/stdc++.h> #define N 100005 #define L ...
- 全面掌握Nginx配置+快速搭建高可用架构 一 Nginx请求限制
三次握手细节 语法: key为分配空间的关键字,以及分配空间的大小 示例: 压力测试工具ab
- 吴裕雄--天生自然 JAVASCRIPT开发学习:变量
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- hive的join优化
“国际大学生节”又称“世界大学生节”.“世界学生日”.“国际学生日”.1946年,世界各国学生代表于布拉格召开全世界学生大会,宣布把每年的11月17日定为“世界大学生节”,以加强全世界大学生的团结和友 ...
- java基础——既然有了字节流,为什么还要有字符流呢?
不管是文件读写还是网络发送接收,信息的最小存储单元都是字节,那为什么I/O流操作要分字节流操作和字符流操作呢? 字符流是由JVM将字节转换得到的,所以这个过程还是非常耗时的,同样假如我们不知道编码方式 ...