-
Notifications
You must be signed in to change notification settings - Fork 0
/
primmaze.cpp
238 lines (214 loc) · 5.31 KB
/
primmaze.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "primmaze.h"
PrimMaze::PrimMaze(int sideLength)
{
this->sideLength = sideLength;
mazeLength = sideLength - 2;
map = new int* [sideLength];//此处进行map行的动态初始化
maze = new int* [sideLength - 1];
pathmap = new int* [sideLength - 1];
for (int i = 0; i < sideLength; i++)
{
map[i] = new int[sideLength];//动态初始化数组的列
memset(map[i], 0, sideLength * sizeof(int));//由于memset只能初始化一维数组,所以在每动态初始化一列的时候填充
}
for (int i = 0; i < mazeLength; i++)
{
maze[i] = new int[mazeLength];
pathmap[i] = new int[mazeLength];
memset(maze[i], 0, mazeLength * sizeof(int));
memset(pathmap[i], 0, mazeLength * sizeof(int));
}
}
PrimMaze::~PrimMaze()
{
}
void PrimMaze::printmap()//打印地图
{
//4右,5下,6左,7上
for (int i = 0; i < mazeLength; i++)
{
for (int j = 0; j < mazeLength; j++)
{
if (maze[i][j] == 0)
{
cout << " ";
}
else if (maze[i][j] == 1)
{
cout << "■";
}
else if (maze[i][j] == 2)
{
cout << "♀";
}
else if (maze[i][j] == 3)
{
cout << "×";
}
else if (maze[i][j] == 4)
{
cout << "↓";
}
else if (maze[i][j] == 5)
{
cout << "→";
}
else if (maze[i][j] == 6)
{
cout << "↑";
}
else if (maze[i][j] == 7)
{
cout << "←";
}
}
cout << endl;
}
}
void PrimMaze::createwall()//初始化整个二维数组为墙
{
for (int i = 0; i < sideLength; i++)
{
map[0][i] = 1;
map[sideLength - 1][i] = 1;
map[i][0] = 1;
map[i][sideLength - 1] = 1;
}
}
void PrimMaze::createmaze()
{
srand((unsigned int)time(NULL));
pointNode seed(2, 2);
pathvector.push_back(seed);
//当墙队列为空时结束循环
while (pathvector.size()) {
//在墙队列中随机取一点
int r = rand() % pathvector.size();
int x = pathvector[r].getX();
int y = pathvector[r].getY();
//判读上下左右四个方向是否为路
int count = 0;
for (int i = x - 1; i < x + 2; i++) {
for (int j = y - 1; j < y + 2; j++) {
if (abs(x - i) + abs(y - j) == 1 && map[i][j] > 0) {
++count;
}
}
}
if (count <= 1) {
map[x][y] = 1;
//在墙队列中插入新的墙
for (int i = x - 1; i < x + 2; i++) {
for (int j = y - 1; j < y + 2; j++) {
if (abs(x - i) + abs(y - j) == 1 && map[i][j] == 0) {
pointNode nowpoint(i, j);
pathvector.push_back(nowpoint);
}
}
}
}
//删除当前墙
pathvector.erase(pathvector.begin() + r);
}
//设置迷宫进出口
for (int i = sideLength - 3; i >= 0; i--) {
if (map[i][sideLength - 3] == 1) {
map[i][sideLength - 2] = 1;
break;
}
}
for (int i = 0; i < mazeLength; i++)
{
for (int j = 0; j < mazeLength; j++)
{
maze[i][j] = abs(map[i + 1][j + 1] - 1);
}
}
}
void PrimMaze::setStart(int x, int y)
{
while (maze[x][y])
{
cout << "输入非法请重新输入:";
cin >> x >> y;
}
startX = x;
startY = y;
maze[startX][startY] = 2;
}
void PrimMaze::calculationEnd()//计算终点
{
for (int i = mazeLength - 1; i >= 0; i--)
{
if (maze[i][mazeLength - 1] == 0)
{
endX = i;
endY = mazeLength - 1;
}
}
maze[endX][endY] = 3;
}
bool PrimMaze::canGo(int x, int y)
{
if (x < 0 && x > mazeLength)
{
return 0;
}
else if (y < 0 && y > mazeLength)
{
}
else if (maze[x][y] == 1)
{
return 0;
}
else if (pathmap[x][y] == 1)
{
return 0;
}
else
{
return 1;
}
}
void PrimMaze::BFScalculation()
{
start = new point;
start->x = startX, start->y = startY;
shortqueue.push(start);
pathmap[start->x][start->y] = 1;
while (shortqueue.size())
{
point* nownode = shortqueue.front();
shortqueue.pop();
p = nownode;
if (p->x == endX && p->y == endY)
{
break;
}
for (int i = 0; i < 4; i++)
{
if (canGo(p->x + dx[i], p->y + dy[i]))
{
newnode = new point;
newnode->x = p->x + dx[i];
newnode->y = p->y + dy[i];
newnode->dir = dir[i];
newnode->parent = p;
shortqueue.push(newnode);
pathmap[newnode->x][newnode->y] = 1;
}
}
}
bfspathnode = p->parent;
}
void PrimMaze::print()
{
for (int i = 0; i < mazeLength; i++)
{
for (int j = 0; j < mazeLength; j++)
{
cout << maze[i][j];
}
cout << endl;
}
}