From 1edbb037d05f50cae9dc6486514dfe21ef4d6d46 Mon Sep 17 00:00:00 2001 From: solovevds Date: Fri, 22 May 2026 21:28:16 +0300 Subject: [PATCH] stage 4 has been completed --- SolovevDS/docs/data/data_for_task2/task2.cpp | 95 +++++++++++++++++--- 1 file changed, 84 insertions(+), 11 deletions(-) diff --git a/SolovevDS/docs/data/data_for_task2/task2.cpp b/SolovevDS/docs/data/data_for_task2/task2.cpp index 2ad321f..05c42a2 100644 --- a/SolovevDS/docs/data/data_for_task2/task2.cpp +++ b/SolovevDS/docs/data/data_for_task2/task2.cpp @@ -1,8 +1,9 @@ #include #include #include -#include +#include /*для ошибок*/ #include +#include /*мерит время*/ class cell{ private: @@ -12,15 +13,15 @@ class cell{ bool isStart; public: - cell(){x=0; y=0; isWall=false; isExit=false; isStart = false;} - cell(int x, int y, bool isWall, bool isExit, bool isStart) - { - this->x = x; - this->y = y; - this->isWall = isWall; - this->isExit = isExit; - this->isStart = isStart; - } + cell() {x=0; y=0; isWall=false; isExit=false; isStart = false;} + cell(int x, int y, bool isWall, bool isExit, bool isStart) + { + this->x = x; + this->y = y; + this->isWall = isWall; + this->isExit = isExit; + this->isStart = isStart; + } bool isPassable() {return !isWall;} @@ -259,6 +260,7 @@ class TextFileMazeBuilder : public MazeBuilder { class PathFindingStrategy { public: virtual cell** findPath(maze* m, cell* start, cell* exit) = 0; + virtual int getVisitedCells() = 0; /*для посещенных клеток*/ virtual ~PathFindingStrategy() {} }; @@ -290,8 +292,16 @@ class PathBuilder { }; class BFSStrategy : public PathFindingStrategy { + private: + int visitedCells; public: + + BFSStrategy() {visitedCells = 0;} + + int getVisitedCells() override {return visitedCells;} + cell** findPath(maze* m, cell* start, cell* exit) override { + visitedCells = 0; int width = m->getWidth(); int height = m->getHeight(); bool** visited = new bool*[width]; @@ -319,6 +329,7 @@ class BFSStrategy : public PathFindingStrategy { while (head < tail) { cell* current = deque[head]; head++; + visitedCells++; if (current == exit) { //сравниваются указатели found = true; @@ -367,8 +378,15 @@ class BFSStrategy : public PathFindingStrategy { }; class DFSStrategy : public PathFindingStrategy { + private: + int visitedCells; public: + DFSStrategy() {visitedCells = 0;} + + int getVisitedCells() override {return visitedCells;} + cell** findPath(maze* m, cell* start, cell* exit) override { + visitedCells = 0; int width = m->getWidth(); int height = m->getHeight(); bool** visited = new bool*[width]; @@ -394,6 +412,7 @@ class DFSStrategy : public PathFindingStrategy { while (top > 0) { top--; cell* current = stack[top]; + visitedCells++; if (current == exit) { //сравниваются указатели found = true; @@ -444,16 +463,22 @@ class DFSStrategy : public PathFindingStrategy { class AStarStrategy : public PathFindingStrategy { private: int heuristic(cell* current, cell* exit) {return std::abs(current->getX() - exit->getX()) + std::abs(current->getY() - exit->getY());} + int visitedCells; public: + AStarStrategy() {visitedCells = 0;} + + int getVisitedCells() override {return visitedCells;} + cell** findPath(maze* m, cell* start, cell* exit) override { + visitedCells = 0; int width = m->getWidth(); int height = m->getHeight(); bool** closed = new bool*[width]; /*клетка [x][y] посещена (да/нет)*/ bool** inOpen = new bool*[width]; /*клетка [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]; @@ -511,6 +536,7 @@ class AStarStrategy : public PathFindingStrategy { if (current == exit) { found = true; + visitedCells++; /*чтоб выход засчитывался*/ break; } @@ -522,6 +548,7 @@ class AStarStrategy : public PathFindingStrategy { inOpen[cx][cy] = false; closed[cx][cy] = true; + visitedCells++; 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 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(){