Я новичок в C++, но не в программировании — я уверен, что это логическая ошибка, но любая помощь приветствуется.
Код: Выделить всё
#include
#include
#include
#include
class Node {
public:
int x;
int y;
Node* parent;
Node(int x, int y, Node* parent = nullptr) {
this->x = x;
this->y = y;
this->parent = parent;
};
};
std::vector generate_full_maze(int columns, int rows) {
std::vector maze(columns, std::vector(rows));
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
maze[i][j] = new Node(i, j);
}
}
return maze;
}
void print_maze(std::vector maze) {
for (int i = 0; i < maze.size(); i++) {
for (int j = 0; j < maze[i].size(); j++) {
// Print current cell and parent cell
std::cout x 0) {
neighbours.push_back(maze[current_cell->y][current_cell->x - 1]);
}
if (current_cell->x < maxx - 1) {
neighbours.push_back(maze[current_cell->y][current_cell->x + 1]);
}
if (current_cell->y > 0) {
neighbours.push_back(maze[current_cell->y - 1][current_cell->x]);
}
if (current_cell->y < maxy - 1) {
neighbours.push_back(maze[current_cell->y + 1][current_cell->x]);
}
std::random_shuffle(neighbours.begin(), neighbours.end());
for (size_t i = 0; i < neighbours.size(); i++) {
if (!is_visited(neighbours[i]->x, neighbours[i]->y, visited)) {
maze[neighbours[i]->y][neighbours[i]->x]->parent = current_cell;
visited.push_back(neighbours[i]);
stack.push(neighbours[i]);
}
}
}
}
int main() {
int x = 5;
int y = 5;
std::vector maze = generate_full_maze(x, y);
// Initialize the starting node
Node* startNode = new Node(0, 0);
maze[0][0] = startNode;
carve_walls(x, y, maze);
print_maze(maze);
// Clean up maze memory
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
delete maze[i][j];
};
};
return 0;
}
Пример вывода:
Код: Выделить всё
0,0 Parent: None
0,1 Parent: 0,0
0,2 Parent: 0,3
0,3 Parent: 0,2
0,4 Parent: 1,4
1,0 Parent: 0,0
1,1 Parent: 1,0
1,2 Parent: 0,2
1,3 Parent: 2,3
1,4 Parent: 0,4
2,0 Parent: 1,0
2,1 Parent: 2,2
2,2 Parent: 3,2
2,3 Parent: 2,4
2,4 Parent: 2,3
3,0 Parent: 4,0
3,1 Parent: 3,0
3,2 Parent: 2,2
3,3 Parent: 3,2
3,4 Parent: 2,4
4,0 Parent: 3,0
4,1 Parent: 4,2
4,2 Parent: 4,1
4,3 Parent: 4,2
4,4 Parent: 3,4
[img]https://i .sstatic.net/tATdEfyf.png[/img]
Подробнее здесь: https://stackoverflow.com/questions/793 ... ot-working