原文: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++随机迷宫生成[转载]的更多相关文章

  1. 利用纯JS和HTML Canvas生成随机迷宫过程中产生的有趣的事情

    上效果图: #先看生成随机迷宫的代码吧↓ <html> <head> <title>生成随机迷宫v1.0</title> </head> & ...

  2. [迷宫中的算法实践]迷宫生成算法——递归分割算法

    Recursive division method        Mazes can be created with recursive division, an algorithm which wo ...

  3. 随机Prim法创建随机迷宫(C#实现)

    因为这两天想参加一个比赛,所以就在上网找素材,刚好看到了迷宫生成,就决定拿这个开刀了. 参考的原文地址为(来源页面) 源地址中是使用AS实现的,没学过AS,所以直接不会运行,于是就自己根据原文的概念进 ...

  4. 一个比较全面的java随机数据生成工具包

    最近,由于一个项目的原因需要使用一些随机数据做测试,于是写了一个随机数据生成工具,ExtraRanom.可以看成是Java官方Random类的扩展,主要用于主要用于测试程序.生成密码.设计抽奖程序等情 ...

  5. 利用Java随机,生成随机学生数据

    为模拟向数据库中大量插入学生数据(注:此处应该用PreparedStatement.batchUpdate等批处理提高效率)的情形,通过Java随机来生成学生数据. 一.要生成的学生数据 studen ...

  6. Clojure——学习迷宫生成

    背景 初学clojure,想着看一些算法来熟悉clojure语法及相关算法实现. 找到一个各种语言生成迷宫的网站:http://rosettacode.org/wiki/Maze_generation ...

  7. 随机数据生成与对拍【c++版,良心讲解】

    10.7更新:见最下面 离NOIP2018没剩多长时间了,我突然发现我连对拍还不会,于是赶紧到网上找资料,找了半天发现了一个特别妙的程序,用c++写的! 不过先讲讲随机数据生成吧. 很简单,就是写一个 ...

  8. solr的随机排序 【转载】

    原文地址:http://blog.csdn.net/duck_genuine/article/details/8477336 有这样的一种需求,想从索引库里随机取出4条记录. 在 schema.xml ...

  9. linux shell 随机字符生成单词

    #!/bin/sh #生成随机5个单词 filecount= wordcount= flag= #-lt -le -gt -ge -eq #while [ $f -lt $filecount ]; # ...

随机推荐

  1. yagmail四行代码发送邮件

    yagmail四行代码发送邮件 import yagmail # 链接邮箱服务器 yag = yagmail.SMTP(user="xxxx@163.com", password= ...

  2. K8S Kubernetes 简单介绍 转自 http://time-track.cn/kubernetes-trial.html Kubernetes初体验

    这段时间学习了一下 git jenkins docker  最近也在看  Kubernetes  感觉写得很赞  也是对自己对于K8S 有了进一步得理解  感谢 倪 大神得Blog 也希望看到这篇Bl ...

  3. css 径向渐变

    .example { width: 150px; height: 80px; background: -webkit-radial-gradient(red, green, blue); /* Saf ...

  4. Mac安装vue产生错误

    npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/webpack/node_modules/_ ...

  5. (转) Windows如何区分鼠标双击和两次单击

    Windows如何区分鼠标双击和两次单击 http://lbsloveldm.blog.hexun.com/12212875_d.html 在Windows平台上,鼠标左键的按下.松开.快速的两次点击 ...

  6. Spring Tools 4 STS没有创建Dynamic Web Project的选项 以及 Spring Tools 4 STS New 菜单没有Spring Bean Configuration File选项

    Spring Tools 4 STS没有创建Dynamic Web Project的选项 STS4默认不带Dynamic Web Project插件. 解决方法:1.打开:Help 选择 Instal ...

  7. 04-String——课后动手动脑

    1.请运行以下示例代码StringPool.java,查看输出结果.如何解释这样的输出结果?从中你能总结出什么? public class StringPool { public static voi ...

  8. Python 中 使用 HTMLTestRunner 模块生成测试报告

     使用 HTMLTestRunner 模块可以生成测试报告,但是系统自带的报告不详细,不好看,所以找了一份详细的报告 HTMLTestRunner 模板,直接导入就能使用 两种方法生成HTML报告,都 ...

  9. 吴裕雄--天生自然TensorFlow2教程:张量限幅

    import tensorflow as tf a = tf.range(10) a # a中小于2的元素值为2 tf.maximum(a, 2) # a中大于8的元素值为8 tf.minimum(a ...

  10. 兼容iphonex底部那个

    @media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ra ...