forked from neolay/HeartMaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.cpp
265 lines (246 loc) · 4.98 KB
/
Maze.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "stdafx.h"
#include "Maze.h"
bool visited[N][M];
bool mask[N][M];
bool area[N][M];
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
int main()
{
Maze maze[N][M];
Block in, out;
initMaze(maze, in, out);
findPath(maze, in, out);
return 0;
}
void initMaze(Maze maze[N][M], Block &in, Block &out)
{
initHeart();
srand((unsigned)time(NULL));
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (area[i][j] && !(area[i][j - 1] && area[i][j + 1] && area[i - 1][j] && area[i + 1][j]))
maze[i][j] = OUTWALL;
else if (area[i][j] && i % 2 == 0 && j % 2 == 0)
maze[i][j] = ROAD;
else if (area[i][j])
maze[i][j] = INWALL;
else
maze[i][j] = EMPTYWALL;
visited[i][j] = false;
mask[i][j] = true;
}
}
in.x = 1;
in.y = 6;
out.x = 27;
out.y = 36;
maze[in.x][in.y] = maze[out.x][out.y] = ROAD;
SqQueue S;
Block start;
initQueue(S);
start.x = 2;
start.y = 6;
enQueue(S, start);
visited[start.x][start.y] = true;
clearMask(start.x, start.y);
while (!isEmpty(S))
{
Block curBlock;
deQueue(S, curBlock);
for (int i = 0; i < 4; i++)
{
int nextBlockX = curBlock.x + di[i][0] * 2;
int nextBlockY = curBlock.y + di[i][1] * 2;
if (inMaze(nextBlockX, nextBlockY) && maze[nextBlockX][nextBlockY] == ROAD && !visited[nextBlockX][nextBlockY])
{
Block nextBlock;
nextBlock.x = nextBlockX;
nextBlock.y = nextBlockY;
enQueue(S, nextBlock);
maze[curBlock.x + di[i][0]][curBlock.y + di[i][1]] = ROAD;
visited[nextBlockX][nextBlockY] = true;
clearMask(nextBlockX, nextBlockY);
updateMaze(maze);
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (maze[i][j] == OUTWALL)
{
mask[i][j] = false;
updateMaze(maze);
}
}
}
}
void findPath(Maze maze[N][M], Block in, Block out)
{
Block block;
SqStack S;
initStack(S);
int curBlockX = in.x;
int curBlockY = in.y;
int step = 1;
do {
if (inMaze(curBlockX, curBlockY) && maze[curBlockX][curBlockY] == ROAD)
{
initBlock(block, curBlockX, curBlockY, EAST);
push(S, block);
maze[curBlockX][curBlockY] = block.di;
updateMaze(maze);
if (curBlockX == out.x&&curBlockY == out.y)
{
printf("\n寻路成功\n共%d步", step);
Sleep(5000);
system("cls");
return;
}
curBlockX = nextBlockX(block.x, block.di);
curBlockY = nextBlockY(block.y, block.di);
step++;
}
else
{
pop(S, block);
if (block.di < NORTH)
{
block.di++;
maze[block.x][block.y] = block.di;
push(S, block);
curBlockX = nextBlockX(block.x, block.di);
curBlockY = nextBlockY(block.y, block.di);
updateMaze(maze);
}
else
{
maze[block.x][block.y] = BACK;
updateMaze(maze);
step--;
}
}
} while (!isEmpty(S));
printf("寻路失败");
}
void initBlock(Block &block, int x, int y, int di)
{
block.x = x;
block.y = y;
block.di = di;
}
void showBlock(Maze maze[N][M])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (mask[i][j] == true)
printf(" ");
else if (maze[i][j] == INWALL)
printf("█");
else if (maze[i][j] == OUTWALL)
printf("█");
else if (maze[i][j] == EMPTYWALL)
printf(" ");
else if (maze[i][j] == ROAD)
printf(" ");
else if (maze[i][j] == BACK)
printf("□");
else if (maze[i][j] == EAST)
printf("→");
else if (maze[i][j] == SOUTH)
printf("↓");
else if (maze[i][j] == WEST)
printf("←");
else if (maze[i][j] == NORTH)
printf("↑");
if (j != 0 && j % (M - 1) == 0)
printf("\n");
}
}
}
void updateMaze(Maze maze[N][M])
{
hideCursor();
gotoxy(0, 0);
showBlock(maze);
Sleep(DELAY);
}
int nextBlockX(int a, int di)
{
int x = a;
switch (di)
{
case SOUTH:
x++;
break;
case NORTH:
x--;
break;
default:
break;
}
return x;
}
int nextBlockY(int b, int di)
{
int y = b;
switch (di)
{
case EAST:
y++;
break;
case WEST:
y--;
break;
default:
break;
}
return y;
}
bool inMaze(int x, int y)
{
return x >= 0 && x < N&&y >= 0 && y < M;
}
void clearMask(int x, int y)
{
for (int i = x - 1; i <= x + 1; i++)
for (int j = y - 1; j <= y + 1; j++)
if (inMaze(i, j))
mask[i][j] = false;
}
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void hideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void initHeart()
{
int i = 0, j = 0;
for (float y = 1.2f; y > -1.0f; y -= 0.06f)
{
for (float x = -1.1f; x < 1.2f; x += 0.05f)
{
float a = x * x + y * y - 1;
if (a * a * a - x * x * y * y * y <= 0.0f)
area[i][j] = true;
else
area[i][j] = false;
j++;
}
i++;
j = 0;
}
}