原文: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. 苏州大学ICPC集训队新生赛第二场

    A - Score UVA - 1585 水 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin> ...

  2. UVA - 11925 Generating Permutations(生成排列)(构造)

    题意:将序列1,2,3,……,n,用不超过2n^2次操作,通过下列操作变成给定序列.(1<=n<=300) 1.交换前两个元素 2.将第一个元素移到最后 分析:因为将序列变成升序更容易操作 ...

  3. Ubuntu16.04 在Windows10 系统下的安装(双系统)

    楼主最近升级了一个固态+8G双通道内存条,重装了一下win10和ubuntu系统,过程中遇到一些问题,push上来供自己和大家参考.比较好用的博客教程直接贴链接. 一.win10系统 学校有正版软件许 ...

  4. blkid命令 获取文件系统类型、UUID

    在Linux下可以使用blkid命令对查询设备上所采用文件系统类型进行查询.blkid主要用来对系统的块设备(包括交换分区)所使用的文件系统类型.LABEL.UUID等信息进行查询.要使用这个命令必须 ...

  5. TP多条件查询实例

    where条件查询,时间范围查询 $condition = [ ['member_id', '=', $member_id] ]; if($type) { $condition[] = ['type' ...

  6. 渗透测试 - HPP数据污染 - 原理 | 场景

    Web服务器 参数获取函数    获取到的参数 PHP/Apache   $_GET(“par”)  Last JSP/Tomcat   Request.getParameter(“par”)    ...

  7. clion 的 安装 变量配置的 搬运工(有点基础应该能看 大家看不懂 就是我自己看 哈哈哈哈哈哈)

    1  自行安装一个  clion 2 https://blog.csdn.net/u013023297/article/details/80723847   mingw  的配置    第二篇好像当时 ...

  8. js数组,字符转换;key_value获取

    js 数组转字符串js 字符串转数组arr.join(",");String.split(","); 对象的所有keys Object.keys(val)对象的 ...

  9. 软件设计之基于Java的连连看小游戏(三)——所有功能的实现

    新年快乐!期末接二连三的考试实在太忙了忘记连连看没有更新完,今天想要学习生信时才发现.所以这次直接把连连看所有功能全部放上. 在传统的连连看的基础上,我增加了上传头像的功能,即可以自行上传图片作为游戏 ...

  10. Java模板引擎之Freemarker 学习笔记 一

    什么是Freemarker Freemarker是模板引擎,不是Web框架,只是视图层的组件,官网是 https://freemarker.apache.org/ Freemarker原理 数据模型+ ...