forked from UNN/2026-rff_mp
added BFS and DFS
This commit is contained in:
parent
52dd19b535
commit
5a978707d8
|
|
@ -147,8 +147,9 @@ class maze{
|
|||
}
|
||||
|
||||
cell* getStart() {return start;}
|
||||
|
||||
cell* getExit() {return exit;}
|
||||
int getWidth() {return width;}
|
||||
int getHeight() {return height;}
|
||||
};
|
||||
|
||||
class MazeBuilder {
|
||||
|
|
@ -253,6 +254,201 @@ class TextFileMazeBuilder : public MazeBuilder {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
class PathFindingStrategy {
|
||||
public:
|
||||
virtual cell** findPath(maze* m, cell* start, cell* exit) = 0;
|
||||
virtual ~PathFindingStrategy() {}
|
||||
};
|
||||
|
||||
class PathBuilder {
|
||||
public:
|
||||
static cell** buildPath(cell* start, cell* exit, cell*** parent) {
|
||||
int length = 0;
|
||||
cell* current = exit;
|
||||
|
||||
while (current != nullptr) {
|
||||
length++;
|
||||
if (current == start)
|
||||
break;
|
||||
current = parent[current->getX()][current->getY()];
|
||||
}
|
||||
|
||||
cell** path = new cell*[length + 1];
|
||||
current = exit;
|
||||
|
||||
for (int i = length - 1; i >= 0; i--) {
|
||||
path[i] = current;
|
||||
if (current == start)
|
||||
break;
|
||||
current = parent[current->getX()][current->getY()];
|
||||
}
|
||||
path[length] = nullptr;
|
||||
return path;
|
||||
}
|
||||
};
|
||||
|
||||
class BFSStrategy : public PathFindingStrategy {
|
||||
public:
|
||||
cell** findPath(maze* m, cell* start, cell* exit) override {
|
||||
int width = m->getWidth();
|
||||
int height = m->getHeight();
|
||||
bool** visited = new bool*[width];
|
||||
cell*** parent = new cell**[width];
|
||||
|
||||
for (int x = 0; x < width; x++) {
|
||||
visited[x] = new bool[height];
|
||||
parent[x] = new cell*[height];
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
visited[x][y] = false;
|
||||
parent[x][y] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
cell** deque = new cell*[width * height];
|
||||
int head = 0;
|
||||
int tail = 0;
|
||||
|
||||
deque[tail] = start;
|
||||
tail++;
|
||||
visited[start->getX()][start->getY()] = true;
|
||||
bool found = false;
|
||||
|
||||
while (head < tail) {
|
||||
cell* current = deque[head];
|
||||
head++;
|
||||
|
||||
if (current == exit) { //сравниваются указатели
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
cell** neighbors = m->getNeighbors(current);
|
||||
|
||||
for (int i = 0; neighbors[i] != nullptr; i++) {
|
||||
cell* next = neighbors[i];
|
||||
|
||||
int nx = next->getX();
|
||||
int ny = next->getY();
|
||||
|
||||
if (!visited[nx][ny]) {
|
||||
visited[nx][ny] = true;
|
||||
parent[nx][ny] = current;
|
||||
|
||||
deque[tail] = next;
|
||||
tail++;
|
||||
}
|
||||
}
|
||||
delete[] neighbors;
|
||||
}
|
||||
|
||||
cell** path;
|
||||
|
||||
if (found) {
|
||||
path = PathBuilder::buildPath(start, exit, parent);
|
||||
}
|
||||
else {
|
||||
path = new cell*[1];
|
||||
path[0] = nullptr;
|
||||
}
|
||||
|
||||
delete[] deque;
|
||||
|
||||
for (int x = 0; x < width; x++) {
|
||||
delete[] visited[x];
|
||||
delete[] parent[x];
|
||||
}
|
||||
delete[] visited;
|
||||
delete[] parent;
|
||||
return path;
|
||||
}
|
||||
};
|
||||
|
||||
class DFSStrategy : public PathFindingStrategy {
|
||||
public:
|
||||
cell** findPath(maze* m, cell* start, cell* exit) override {
|
||||
int width = m->getWidth();
|
||||
int height = m->getHeight();
|
||||
bool** visited = new bool*[width];
|
||||
cell*** parent = new cell**[width];
|
||||
|
||||
|
||||
cell** stack = new cell*[width * height];
|
||||
int top = 0;
|
||||
for (int x = 0; x < width; x++) {
|
||||
visited[x] = new bool[height];
|
||||
parent[x] = new cell*[height];
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
visited[x][y] = false;
|
||||
parent[x][y] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
stack[top] = start;
|
||||
top++;
|
||||
visited[start->getX()][start->getY()] = true;
|
||||
bool found = false;
|
||||
|
||||
while (top > 0) {
|
||||
top--;
|
||||
cell* current = stack[top];
|
||||
|
||||
if (current == exit) { //сравниваются указатели
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
cell** neighbors = m->getNeighbors(current);
|
||||
|
||||
for (int i = 0; neighbors[i] != nullptr; i++) {
|
||||
cell* next = neighbors[i];
|
||||
|
||||
int nx = next->getX();
|
||||
int ny = next->getY();
|
||||
|
||||
if (!visited[nx][ny]) {
|
||||
visited[nx][ny] = true;
|
||||
parent[nx][ny] = current;
|
||||
|
||||
stack[top] = next;
|
||||
top++;
|
||||
}
|
||||
}
|
||||
delete[] neighbors;
|
||||
}
|
||||
|
||||
cell** path;
|
||||
|
||||
if (found) {
|
||||
path = PathBuilder::buildPath(start, exit, parent);
|
||||
}
|
||||
else {
|
||||
path = new cell*[1];
|
||||
path[0] = nullptr;
|
||||
}
|
||||
|
||||
delete[] stack;
|
||||
|
||||
for (int x = 0; x < width; x++) {
|
||||
delete[] visited[x];
|
||||
delete[] parent[x];
|
||||
}
|
||||
delete[] visited;
|
||||
delete[] parent;
|
||||
return path;
|
||||
}
|
||||
};
|
||||
|
||||
class AStarStrategy : public PathFindingStrategy {
|
||||
public:
|
||||
cell** findPath(maze* m, cell* start, cell* exit) override {
|
||||
// здесь будет алгоритм A*
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user