From 8dafdab90e5a22213d6d86dd230222ae27143ec0 Mon Sep 17 00:00:00 2001 From: solovevds Date: Wed, 20 May 2026 01:17:12 +0300 Subject: [PATCH] 1 stage has been complited --- SolovevDS/docs/data/data_for_task2/task2.cpp | 115 +++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 SolovevDS/docs/data/data_for_task2/task2.cpp diff --git a/SolovevDS/docs/data/data_for_task2/task2.cpp b/SolovevDS/docs/data/data_for_task2/task2.cpp new file mode 100644 index 0000000..6e97642 --- /dev/null +++ b/SolovevDS/docs/data/data_for_task2/task2.cpp @@ -0,0 +1,115 @@ +class cell{ + private: + int x, y; + bool isWall; + bool isExit; + 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; + } + + bool isPassable() {return !isWall;} + + void setStart(bool value) {isStart = value;} + void setExit(bool value) {isExit = value;} + void setX(int x) {this->x = x;} + void setY(int y) {this->y = y;} + void setIsWall(bool isWall) {this->isWall = isWall;} + + + int getX() {return x;} + int getY() {return y;} + bool getIsWall() {return isWall;} + bool getIsExit() {return isExit;} + bool getIsStart() {return isStart;} + +}; + + +class maze{ + private: + int width; + int height; + cell** matrix; + + cell* start; + cell* exit; + + public: + maze(int width, int height, int startX, int startY, int exitX, int exitY) + { + this->width = width; + this->height = height; + this->start = nullptr; + this->exit = nullptr; + + matrix = new cell*[width]; + for (int i = 0; i < width; ++i) + matrix[i] = new cell[height]; + + matrix[startX][startY].setStart(true); + matrix[exitX][exitY].setExit(true); + + start = &matrix[startX][startY]; + exit = &matrix[exitX][exitY]; + } + + ~maze() + { + for (int i = 0; i < width; ++i) + delete[] matrix[i]; + delete[] matrix; + } + + cell* getCell(int x, int y) { + if (x < 0 || x >= width || y < 0 || y >= height) + return nullptr; + + return &matrix[x][y]; + } + + cell** getNeighbors(cell* current) { /*ДЕЛАТЬ delete[] neighbors; !!!!!!!!!!!!!!!*/ + cell** neighbors = new cell*[5]; + int count = 0; + + int x = current->getX(); + int y = current->getY(); + + cell* up = getCell(x, y - 1); + cell* down = getCell(x, y + 1); + cell* left = getCell(x - 1, y); + cell* right = getCell(x + 1, y); + + if (up != nullptr && up->isPassable()) { + neighbors[count] = up; + count++;} + + if (down != nullptr && down->isPassable()) { + neighbors[count] = down; + count++;} + + if (left != nullptr && left->isPassable()) { + neighbors[count] = left; + count++;} + + if (right != nullptr && right->isPassable()) { + neighbors[count] = right; + count++;} + + neighbors[count] = nullptr; + + return neighbors; + } + + cell* getStart() {return start;} + + cell* getExit() {return exit;} +}; \ No newline at end of file