-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmazemania_1.cpp
106 lines (94 loc) · 2.88 KB
/
mazemania_1.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
#include <iostream>
#include <vector>
#include <stack>
#include<ctime>
#define N 10
#define M 10
struct Cell {
int x, y;
bool visited;
bool walls[4]; // top, right, bottom, left
};
std::vector<std::vector<Cell>> grid(N, std::vector<Cell>(M));
std::stack<Cell*> stack;
void removeWall(Cell& a, Cell& b) {
int dx = a.x - b.x;
if (dx == 1) {
a.walls[3] = false;
b.walls[1] = false;
} else if (dx == -1) {
a.walls[1] = false;
b.walls[3] = false;
}
int dy = a.y - b.y;
if (dy == 1) {
a.walls[0] = false;
b.walls[2] = false;
} else if (dy == -1) {
a.walls[2] = false;
b.walls[0] = false;
}
}
void generateMaze() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
grid[i][j].x = i;
grid[i][j].y = j;
grid[i][j].visited = false;
for (int k = 0; k < 4; k++)
grid[i][j].walls[k] = true;
}
}
Cell* start = &grid[0][0];
start->visited = true;
stack.push(start);
while (!stack.empty()) {
Cell* current = stack.top();
std::vector<Cell*> unvisitedNeighbors;
// Check all four neighbors
if (current->y > 0 && !grid[current->x][current->y - 1].visited)
unvisitedNeighbors.push_back(&grid[current->x][current->y - 1]);
if (current->y < M - 1 && !grid[current->x][current->y + 1].visited)
unvisitedNeighbors.push_back(&grid[current->x][current->y + 1]);
if (current->x > 0 && !grid[current->x - 1][current->y].visited)
unvisitedNeighbors.push_back(&grid[current->x - 1][current->y]);
if (current->x < N - 1 && !grid[current->x + 1][current->y].visited)
unvisitedNeighbors.push_back(&grid[current->x + 1][current->y]);
if (!unvisitedNeighbors.empty()) {
Cell* chosen = unvisitedNeighbors[rand() % unvisitedNeighbors.size()];
removeWall(*current, *chosen);
chosen->visited = true;
stack.push(chosen);
} else {
stack.pop();
}
}
}
void printMaze() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (i == 0 && j == 0)
std::cout << "S "; // Start position
else if (i == N - 1 && j == M - 1 && grid[i][j].walls[0])
std::cout << "E "; // End position
else
std::cout << (grid[i][j].walls[0] ? "# " : " ");
}
std::cout << "\n";
for (int j = 0; j < M; j++) {
if (i == N - 1 && j == M - 1)
continue;
else {
std::cout << (grid[i][j].walls[3] ? "#" : " ");
std::cout << (grid[i][j].walls[1] ? "#" : " ");
}
}
std::cout << "\n";
}
}
int main() {
srand(time(0));
generateMaze();
printMaze();
return 0;
}