stage 4 has been completed

This commit is contained in:
solovevds 2026-05-22 21:28:16 +03:00
parent 4d0d97116e
commit 1edbb037d0

View File

@ -1,8 +1,9 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <stdexcept> #include <stdexcept> /*для ошибок*/
#include <cmath> #include <cmath>
#include <chrono> /*мерит время*/
class cell{ class cell{
private: private:
@ -12,15 +13,15 @@ class cell{
bool isStart; bool isStart;
public: public:
cell(){x=0; y=0; isWall=false; isExit=false; isStart = false;} cell() {x=0; y=0; isWall=false; isExit=false; isStart = false;}
cell(int x, int y, bool isWall, bool isExit, bool isStart) cell(int x, int y, bool isWall, bool isExit, bool isStart)
{ {
this->x = x; this->x = x;
this->y = y; this->y = y;
this->isWall = isWall; this->isWall = isWall;
this->isExit = isExit; this->isExit = isExit;
this->isStart = isStart; this->isStart = isStart;
} }
bool isPassable() {return !isWall;} bool isPassable() {return !isWall;}
@ -259,6 +260,7 @@ class TextFileMazeBuilder : public MazeBuilder {
class PathFindingStrategy { class PathFindingStrategy {
public: public:
virtual cell** findPath(maze* m, cell* start, cell* exit) = 0; virtual cell** findPath(maze* m, cell* start, cell* exit) = 0;
virtual int getVisitedCells() = 0; /*для посещенных клеток*/
virtual ~PathFindingStrategy() {} virtual ~PathFindingStrategy() {}
}; };
@ -290,8 +292,16 @@ class PathBuilder {
}; };
class BFSStrategy : public PathFindingStrategy { class BFSStrategy : public PathFindingStrategy {
private:
int visitedCells;
public: public:
BFSStrategy() {visitedCells = 0;}
int getVisitedCells() override {return visitedCells;}
cell** findPath(maze* m, cell* start, cell* exit) override { cell** findPath(maze* m, cell* start, cell* exit) override {
visitedCells = 0;
int width = m->getWidth(); int width = m->getWidth();
int height = m->getHeight(); int height = m->getHeight();
bool** visited = new bool*[width]; bool** visited = new bool*[width];
@ -319,6 +329,7 @@ class BFSStrategy : public PathFindingStrategy {
while (head < tail) { while (head < tail) {
cell* current = deque[head]; cell* current = deque[head];
head++; head++;
visitedCells++;
if (current == exit) { //сравниваются указатели if (current == exit) { //сравниваются указатели
found = true; found = true;
@ -367,8 +378,15 @@ class BFSStrategy : public PathFindingStrategy {
}; };
class DFSStrategy : public PathFindingStrategy { class DFSStrategy : public PathFindingStrategy {
private:
int visitedCells;
public: public:
DFSStrategy() {visitedCells = 0;}
int getVisitedCells() override {return visitedCells;}
cell** findPath(maze* m, cell* start, cell* exit) override { cell** findPath(maze* m, cell* start, cell* exit) override {
visitedCells = 0;
int width = m->getWidth(); int width = m->getWidth();
int height = m->getHeight(); int height = m->getHeight();
bool** visited = new bool*[width]; bool** visited = new bool*[width];
@ -394,6 +412,7 @@ class DFSStrategy : public PathFindingStrategy {
while (top > 0) { while (top > 0) {
top--; top--;
cell* current = stack[top]; cell* current = stack[top];
visitedCells++;
if (current == exit) { //сравниваются указатели if (current == exit) { //сравниваются указатели
found = true; found = true;
@ -444,16 +463,22 @@ class DFSStrategy : public PathFindingStrategy {
class AStarStrategy : public PathFindingStrategy { class AStarStrategy : public PathFindingStrategy {
private: private:
int heuristic(cell* current, cell* exit) {return std::abs(current->getX() - exit->getX()) + std::abs(current->getY() - exit->getY());} int heuristic(cell* current, cell* exit) {return std::abs(current->getX() - exit->getX()) + std::abs(current->getY() - exit->getY());}
int visitedCells;
public: public:
AStarStrategy() {visitedCells = 0;}
int getVisitedCells() override {return visitedCells;}
cell** findPath(maze* m, cell* start, cell* exit) override { cell** findPath(maze* m, cell* start, cell* exit) override {
visitedCells = 0;
int width = m->getWidth(); int width = m->getWidth();
int height = m->getHeight(); int height = m->getHeight();
bool** closed = new bool*[width]; /*клетка [x][y] посещена (да/нет)*/ bool** closed = new bool*[width]; /*клетка [x][y] посещена (да/нет)*/
bool** inOpen = new bool*[width]; /*клетка [x][y] имеет потенциал к посещению (да/нет)*/ bool** inOpen = new bool*[width]; /*клетка [x][y] имеет потенциал к посещению (да/нет)*/
int** gScore = new int*[width]; /* до клетка [x][y] от старта gSchore[x][y] шагов*/ int** gScore = new int*[width]; /* до клетка [x][y] от старта gSchore[x][y] шагов*/
int** fScore = new int*[width]; /*f = h + g, где g - эвристика клетки[x][y]*/ int** fScore = new int*[width]; /*f = h + g, где h - эвристика клетки[x][y]*/
cell*** parent = new cell**[width]; cell*** parent = new cell**[width];
@ -511,6 +536,7 @@ class AStarStrategy : public PathFindingStrategy {
if (current == exit) { if (current == exit) {
found = true; found = true;
visitedCells++; /*чтоб выход засчитывался*/
break; break;
} }
@ -522,6 +548,7 @@ class AStarStrategy : public PathFindingStrategy {
inOpen[cx][cy] = false; inOpen[cx][cy] = false;
closed[cx][cy] = true; closed[cx][cy] = true;
visitedCells++;
cell** neighbors = m->getNeighbors(current); cell** neighbors = m->getNeighbors(current);
@ -582,6 +609,52 @@ class AStarStrategy : public PathFindingStrategy {
} }
}; };
class SearchStats {
public:
double timeMs;
int visitedCells;
int pathLength;
SearchStats(double timeMs, int visitedCells, int pathLength) {
this->timeMs = timeMs;
this->visitedCells = visitedCells;
this->pathLength = pathLength;
}
};
class MazeSolver{
private:
maze* labirint;
PathFindingStrategy* strategy;
public:
MazeSolver(maze* labirint) {this->labirint = labirint;}
void setStrategy(PathFindingStrategy* strategy){
this->strategy = strategy;
}
SearchStats solve(){
auto start = std::chrono::high_resolution_clock::now();
cell** path = strategy->findPath(labirint,labirint->getStart(),labirint->getExit());
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration = end - start;
int pathLength = 0;
if (path[0] != nullptr)
while (path[pathLength] != nullptr) {pathLength++;}
int visitedCells = 0;
visitedCells = strategy->getVisitedCells();
delete[] path;
SearchStats stats(duration.count(), visitedCells, pathLength);
return stats;
}
};
int main(){ int main(){