当前位置:网站首页>Drawing maze EasyX library with recursive backtracking method
Drawing maze EasyX library with recursive backtracking method
2022-07-03 08:37:00 【Zoe_ Cpp】
#include <stdio.h>
#include <easyx.h>
#include <time.h>
int direction = 0;//0 Indicating right , 1 It means down ,2 Means up ,3 Indicating left
int n = 64 * 64 - 1;
struct Space
{
int x;
int y;
Space* bef = NULL;
bool att;//0 Indicates no access to ,1 Indicates that you have visited
};
Space space[64][64];
int x = 1;
int y = 1;
int z = 1;
bool ifHaveWay()
{
if (space[x - 1][y].att && space[x + 1][y].att && space[x][y - 1].att && space[x][y + 1].att)
return 0;
else
return 1;
}
void RandDirection()
{
while (1)// Get directions
{
direction = rand() % 4;
if (direction == 3 && space[x - 1][y].att)
continue;
if (direction == 0 && space[x + 1][y].att)
continue;
if (direction == 2 && space[x][y - 1].att)
continue;
if (direction == 1 && space[x][y + 1].att)
continue;
break;
}
}
void WallBreak()
{
setlinecolor(BLACK);
if (direction == 1)
line(x * 10 + 1, y * 10, x * 10 + 9, y * 10);
if (direction == 0)
line(x * 10, y * 10 + 1, x * 10, y * 10 + 9);
if (direction == 3)
line(x * 10 + 10, y * 10 + 1, x * 10 + 10, y * 10 + 9);
if (direction == 2)
line(x * 10 + 1, y * 10 + 10, x * 10 + 9, y * 10 + 10);
}
void FoundWay()
{
int tx, ty;
Space* p1 = NULL;
while (x != 1 || y != 1 || z)
{
while (ifHaveWay())// Draw the main line of maze
{
p1 = &space[x][y];
space[x][y].att = 1;
RandDirection();
if (direction == 0)
x++;
if (direction == 1)
y++;
if (direction == 2)
y--;
if (direction == 3)
x--;
WallBreak();
space[x][y].bef = p1;
//Sleep(5);
}
space[x][y].att = 1;
tx = x;
ty = y;
x = space[tx][ty].bef->x;
y = space[tx][ty].bef->y;
z = 0;
}
}
// initialization
void SetGraph()
{
for (int i = 0; i < 64; i++)
{
for (int j = 0; j < 64; j++)
{
space[i][j].x = i;
space[i][j].y = j;
}
}
// Clear the elements in the maze
for (int i = 0; i < 64; i++)
{
for (int j = 0; j < 64; j++)
{
space[i][j].att = 0;
}
}
// Build walls around the maze ( Set to visited )
for (int i = 0; i < 64; i++)
{
space[0][i].att = 1;
space[63][i].att = 1;
space[i][0].att = 1;
space[i][63].att = 1;
}
initgraph(640, 640);
}
void DrawWall()
{
for (int i = 0; i < 640; i += 10)
{
line(i, 0, i, 640);
}
for (int i = 0; i < 640; i += 10)
{
line(0, i, 640, i);
}
solidrectangle(0, 0, 640, 10);
solidrectangle(0, 0, 10, 640);
solidrectangle(630, 0, 640, 640);
solidrectangle(0, 630, 640, 640);
}
int main()
{
srand(time(NULL));
SetGraph();
DrawWall();
FoundWay();
setlinecolor(WHITE);
system("pause");
return 0;
}
Practice effect 
边栏推荐
- Ue5 opencv plug-in use
- LinkList
- [rust notes] 06 package and module
- 【Rust 笔记】07-结构体
- Cesium for unreal quick start - simple scenario configuration
- VIM learning notes from introduction to silk skating
- Simple demo of solving BP neural network by gradient descent method
- Some understandings of 3dfiles
- MySQL 8
- Gradle's method of dynamically modifying APK package name
猜你喜欢

jupyter远程服务器配置以及服务器开机自启

Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial

Collection interface

Unity editor expansion - scrolling list

Message queue for interprocess communication

Use of ue5 QRcode plug-in

单调栈-84. 柱状图中最大的矩形

【Rust笔记】02-所有权

数据分析练习题

matlab神經網絡所有傳遞函數(激活函數)公式詳解
随机推荐
梯度下降法求解BP神经网络的简单Demo
[concurrent programming] thread foundation and sharing between threads
Unity4.3.1 engine source code compilation process
Encoding and decoding of golang URL
简易入手《SOM神经网络》的本质与原理
UE4 source code reading_ Bone model and animation system_ Animation process
Redis cluster series 4
Osgearth target selection
Intersectionpicker in osgearth
Development experience and experience
php-fpm软件的安装+openresty高速缓存搭建
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
Unity editor expansion - scrolling list
Find the intersection of line segments
了解小程序的笔记 2022/7/3
Unity notes 1
redis集群系列四
Mysql容器化(1)Docker安装MySQL
Go resolve ID card
Huawei interview summary during the epidemic