From c3058b9564a2781956c9682abe97f6a33f5c91fc Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Fri, 27 Nov 2020 17:22:53 +0500 Subject: [PATCH 01/15] Added .gitignore. --- .gitignore | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d595c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +*.exe +*.vcxproj.user +*.obj +*.log +*.pdb +*.idb +*.tlog +*.lastbuildstate +*.ilk +*.exe.recipe \ No newline at end of file From ab9df4e4c0c49909eb505017a642de1854c49447 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Fri, 27 Nov 2020 17:25:39 +0500 Subject: [PATCH 02/15] practise start --- Practise_18.11.20/Practise_18.11.20.sln | 31 ++++ Practise_18.11.20/Practise_18.11.20/MCell.cpp | 10 ++ Practise_18.11.20/Practise_18.11.20/MCell.h | 12 ++ Practise_18.11.20/Practise_18.11.20/Maze.cpp | 107 ++++++++++++ Practise_18.11.20/Practise_18.11.20/Maze.h | 25 +++ .../Practise_18.11.20/Practise_18.11.20.cpp | 33 ++++ .../Practise_18.11.20.vcxproj | 153 ++++++++++++++++++ .../Practise_18.11.20.vcxproj.filters | 36 +++++ 8 files changed, 407 insertions(+) create mode 100644 Practise_18.11.20/Practise_18.11.20.sln create mode 100644 Practise_18.11.20/Practise_18.11.20/MCell.cpp create mode 100644 Practise_18.11.20/Practise_18.11.20/MCell.h create mode 100644 Practise_18.11.20/Practise_18.11.20/Maze.cpp create mode 100644 Practise_18.11.20/Practise_18.11.20/Maze.h create mode 100644 Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp create mode 100644 Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj create mode 100644 Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj.filters diff --git a/Practise_18.11.20/Practise_18.11.20.sln b/Practise_18.11.20/Practise_18.11.20.sln new file mode 100644 index 0000000..80515b1 --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30503.244 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Practise_18.11.20", "Practise_18.11.20\Practise_18.11.20.vcxproj", "{00485E1F-6529-4922-892E-50885EF9109E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {00485E1F-6529-4922-892E-50885EF9109E}.Debug|x64.ActiveCfg = Debug|x64 + {00485E1F-6529-4922-892E-50885EF9109E}.Debug|x64.Build.0 = Debug|x64 + {00485E1F-6529-4922-892E-50885EF9109E}.Debug|x86.ActiveCfg = Debug|Win32 + {00485E1F-6529-4922-892E-50885EF9109E}.Debug|x86.Build.0 = Debug|Win32 + {00485E1F-6529-4922-892E-50885EF9109E}.Release|x64.ActiveCfg = Release|x64 + {00485E1F-6529-4922-892E-50885EF9109E}.Release|x64.Build.0 = Release|x64 + {00485E1F-6529-4922-892E-50885EF9109E}.Release|x86.ActiveCfg = Release|Win32 + {00485E1F-6529-4922-892E-50885EF9109E}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2C11CE7B-19BA-4E0E-9174-7851A60AE71B} + EndGlobalSection +EndGlobal diff --git a/Practise_18.11.20/Practise_18.11.20/MCell.cpp b/Practise_18.11.20/Practise_18.11.20/MCell.cpp new file mode 100644 index 0000000..f766211 --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/MCell.cpp @@ -0,0 +1,10 @@ +#include "MCell.h" + +MCell::MCell() { + m_down = false; + m_right = false; +} + +bool MCell::down() const { return m_down; } + +bool MCell::right() const { return m_right; } diff --git a/Practise_18.11.20/Practise_18.11.20/MCell.h b/Practise_18.11.20/Practise_18.11.20/MCell.h new file mode 100644 index 0000000..702ab1d --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/MCell.h @@ -0,0 +1,12 @@ +#pragma once + +class MCell { +private: + MCell(); + bool m_down; + bool m_right; +public: + bool down() const; + bool right() const; + friend class Maze; +}; diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.cpp b/Practise_18.11.20/Practise_18.11.20/Maze.cpp new file mode 100644 index 0000000..46031b9 --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/Maze.cpp @@ -0,0 +1,107 @@ +#include +#include +#include "Maze.h" +using namespace std; + +char GetChar(const int& num) { + switch (num) { + case PrevDown + PrevRight + Down + Right: + return char(197); + case PrevDown + PrevRight + Down: + return char(180); + case PrevDown + PrevRight + Right: + return char(193); + case PrevDown + Down + Right: + return char(195); + case PrevRight + Down + Right: + return char(194); + case PrevDown + PrevRight: + return char(217); + case PrevDown + Down: + return char(179); + case PrevDown + Right: + return char(192); + case PrevRight + Down: + return char(191); + case PrevRight + Right: + return char(196); + case Right + Down: + return char(218); + default: + return '0'; + break; + } +} + +Maze::Maze(int n, int m) { + width = m; + size = n * m; + m_field = new MCell[size]; +} + +Maze::~Maze() { delete[] m_field; } + +const MCell& Maze::cell(int i, int j) const { return m_field[i * width + j]; } + +bool Maze::hasConnection(int i1, int j1, int i2, int j2) { + int ind1 = min(i1, i2); + int ind2 = min(j1, j2); + if (i1 == i2) { + return m_field[ind1 * width + ind2].right(); + } + else if (j2 == j1) { + return m_field[ind1 * width + ind2].down(); + } + else + return false; +} + +bool Maze::makeConnection(int i1, int j1, int i2, int j2) { + int ind1 = min(i1, i2); + int ind2 = min(j1, j2); + if (i1 == i2) { + m_field[ind1 * width + ind2].m_right = true; + return true; + } + else if (j1 == j2) { + m_field[ind1 * width + ind2].m_down = true; + return true; + } + else + return false; +} + +bool Maze::removeConnection(int i1, int j1, int i2, int j2) { + int ind1 = min(i1, i2); + int ind2 = min(j1, j2); + if (i1 == i2) { + m_field[ind1 * width + ind2].m_right = false; + return true; + } + else if (j2 == j1) { + m_field[ind1 * width + ind2].m_down = false; + return true; + } + else + return false; +} + +void Maze::printMaze() const { + for (int n = 0; n < size; ++n) { + int i = n / width; + int j = n % width; + int res = m_field[n].right() << 3 | m_field[n].down() << 2; + if (i - 1 >= 0) + res += m_field[(i - 1) * width + j].down() << 0; + if (j - 1 >= 0) + res += m_field[i * width + j - 1].right() << 1; + if (res == 1 || res == 2 || res == 4 || res == 8) { + cout << '0'; + } + else + cout << GetChar(res); + + if (j + 1 == width) + cout << endl; + } +} \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.h b/Practise_18.11.20/Practise_18.11.20/Maze.h new file mode 100644 index 0000000..f34d2b2 --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/Maze.h @@ -0,0 +1,25 @@ +#pragma once +#include "MCell.h" +#include + +enum MazeElem { + PrevDown = 1 << 0, + PrevRight = 1 << 1, + Down = 1 << 2, + Right = 1 << 3, +}; + +class Maze { +private: + MCell* m_field; + int width; + int size; +public: + Maze(int n, int m); + ~Maze(); + const MCell& cell(int i, int j) const; + bool hasConnection(int i1, int j1, int i2, int j2); + bool makeConnection(int i1, int j1, int i2, int j2); + bool removeConnection(int i1, int j1, int i2, int j2); + void printMaze() const; +}; diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp new file mode 100644 index 0000000..a7211df --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp @@ -0,0 +1,33 @@ +#include +#include "MCell.h" +#include "Maze.h" +using namespace std; + +void ChangeMaze(Maze& maze, const int& width, const int& height) { + int ind_right = 0; + int ind_down = 1; + for (int i = 0; i < width; ++i) { + for (int j = 0; j < height; ++j) { + if (i == width - 1 && j == height - 1) { + break; + } + if (i == ind_right && j == ind_right) { + maze.makeConnection(i, j, i, j + 1); + } + if (i == ind_right && j == ind_down) { + maze.makeConnection(i, j, i + 1, j); + } + } + ++ind_down; + ++ind_right; + } +} + +int main() +{ + const int maze_size = 5; + Maze maze = Maze(maze_size, maze_size); + ChangeMaze(maze, maze_size, maze_size); + auto res = true << 3 | true << 2; + maze.printMaze(); +} diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj new file mode 100644 index 0000000..0018bcc --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {00485e1f-6529-4922-892e-50885ef9109e} + Practise181120 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj.filters b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj.filters new file mode 100644 index 0000000..6f301fe --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file From cf76b89fe4b40d19bed607f896c6cbf5190f27be Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 28 Nov 2020 14:48:40 +0500 Subject: [PATCH 03/15] Removed additional methods; Fixed the .gitignore file. --- .gitignore | 5 ++++- .../Practise_18.11.20/Practise_18.11.20.cpp | 19 ++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 0d595c9..39a43f7 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,7 @@ *.tlog *.lastbuildstate *.ilk -*.exe.recipe \ No newline at end of file +*.exe.recipe +*.vcxproj +*.sln +*.vcxproj.filters \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp index a7211df..898efd9 100644 --- a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp @@ -3,12 +3,16 @@ #include "Maze.h" using namespace std; -void ChangeMaze(Maze& maze, const int& width, const int& height) { +int main() +{ + const int maze_size = 5; + Maze maze = Maze(maze_size, maze_size); + int ind_right = 0; int ind_down = 1; - for (int i = 0; i < width; ++i) { - for (int j = 0; j < height; ++j) { - if (i == width - 1 && j == height - 1) { + for (int i = 0; i < maze_size; ++i) { + for (int j = 0; j < maze_size; ++j) { + if (i == maze_size - 1 && j == maze_size - 1) { break; } if (i == ind_right && j == ind_right) { @@ -21,13 +25,6 @@ void ChangeMaze(Maze& maze, const int& width, const int& height) { ++ind_down; ++ind_right; } -} -int main() -{ - const int maze_size = 5; - Maze maze = Maze(maze_size, maze_size); - ChangeMaze(maze, maze_size, maze_size); - auto res = true << 3 | true << 2; maze.printMaze(); } From dced4f18e263c0037e0e312343a5670e4573b08b Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 28 Nov 2020 14:58:29 +0500 Subject: [PATCH 04/15] Deleted .vcxproj file; Deleted .sln file; Deleted .vcxproj.filters file. --- Practise_18.11.20/Practise_18.11.20.sln | 31 ---- .../Practise_18.11.20.vcxproj | 153 ------------------ .../Practise_18.11.20.vcxproj.filters | 36 ----- 3 files changed, 220 deletions(-) delete mode 100644 Practise_18.11.20/Practise_18.11.20.sln delete mode 100644 Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj delete mode 100644 Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj.filters diff --git a/Practise_18.11.20/Practise_18.11.20.sln b/Practise_18.11.20/Practise_18.11.20.sln deleted file mode 100644 index 80515b1..0000000 --- a/Practise_18.11.20/Practise_18.11.20.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30503.244 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Practise_18.11.20", "Practise_18.11.20\Practise_18.11.20.vcxproj", "{00485E1F-6529-4922-892E-50885EF9109E}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {00485E1F-6529-4922-892E-50885EF9109E}.Debug|x64.ActiveCfg = Debug|x64 - {00485E1F-6529-4922-892E-50885EF9109E}.Debug|x64.Build.0 = Debug|x64 - {00485E1F-6529-4922-892E-50885EF9109E}.Debug|x86.ActiveCfg = Debug|Win32 - {00485E1F-6529-4922-892E-50885EF9109E}.Debug|x86.Build.0 = Debug|Win32 - {00485E1F-6529-4922-892E-50885EF9109E}.Release|x64.ActiveCfg = Release|x64 - {00485E1F-6529-4922-892E-50885EF9109E}.Release|x64.Build.0 = Release|x64 - {00485E1F-6529-4922-892E-50885EF9109E}.Release|x86.ActiveCfg = Release|Win32 - {00485E1F-6529-4922-892E-50885EF9109E}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {2C11CE7B-19BA-4E0E-9174-7851A60AE71B} - EndGlobalSection -EndGlobal diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj deleted file mode 100644 index 0018bcc..0000000 --- a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj +++ /dev/null @@ -1,153 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - Win32Proj - {00485e1f-6529-4922-892e-50885ef9109e} - Practise181120 - 10.0 - - - - Application - true - v142 - Unicode - - - Application - false - v142 - true - Unicode - - - Application - true - v142 - Unicode - - - Application - false - v142 - true - Unicode - - - - - - - - - - - - - - - - - - - - - true - - - false - - - true - - - false - - - - Level3 - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - Level3 - true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj.filters b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj.filters deleted file mode 100644 index 6f301fe..0000000 --- a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.vcxproj.filters +++ /dev/null @@ -1,36 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Исходные файлы - - - Исходные файлы - - - Исходные файлы - - - - - Файлы заголовков - - - Файлы заголовков - - - \ No newline at end of file From ab80e5f311256fc4fcb680ec1530bf517a4cb3f4 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Mon, 30 Nov 2020 10:14:50 +0500 Subject: [PATCH 05/15] Removed the additional GetChar method --- Practise_18.11.20/Practise_18.11.20/Maze.cpp | 42 ++++---------------- Practise_18.11.20/Practise_18.11.20/Maze.h | 19 +++++++-- 2 files changed, 24 insertions(+), 37 deletions(-) diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.cpp b/Practise_18.11.20/Practise_18.11.20/Maze.cpp index 46031b9..ec81ae1 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Maze.cpp @@ -1,42 +1,16 @@ -#include #include +#include #include "Maze.h" using namespace std; -char GetChar(const int& num) { - switch (num) { - case PrevDown + PrevRight + Down + Right: - return char(197); - case PrevDown + PrevRight + Down: - return char(180); - case PrevDown + PrevRight + Right: - return char(193); - case PrevDown + Down + Right: - return char(195); - case PrevRight + Down + Right: - return char(194); - case PrevDown + PrevRight: - return char(217); - case PrevDown + Down: - return char(179); - case PrevDown + Right: - return char(192); - case PrevRight + Down: - return char(191); - case PrevRight + Right: - return char(196); - case Right + Down: - return char(218); - default: - return '0'; - break; - } -} - Maze::Maze(int n, int m) { width = m; size = n * m; m_field = new MCell[size]; + m_dict = { {AllDirection, char(197)}, {UpLeftDown, char(180)}, {UpLeftRight, char(193)}, + {UpDownRight, char(195)}, {LeftDownRight, char(194)}, {UpLeft, char(217)}, + {UpDown, char(179)}, {UpRight, char(192)}, {LeftDown, char(191)}, + {LeftRight, char(196)}, {RightDown, char(218)}, {Up, '0'}, {Down, '0'}, {Left, '0'}, {Right, '0'} }; } Maze::~Maze() { delete[] m_field; } @@ -95,11 +69,11 @@ void Maze::printMaze() const { res += m_field[(i - 1) * width + j].down() << 0; if (j - 1 >= 0) res += m_field[i * width + j - 1].right() << 1; - if (res == 1 || res == 2 || res == 4 || res == 8) { + + if (res == 0 || res == 1 || res == 2 || res == 4 || res == 8) cout << '0'; - } else - cout << GetChar(res); + cout << m_dict.find(res)->second; if (j + 1 == width) cout << endl; diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.h b/Practise_18.11.20/Practise_18.11.20/Maze.h index f34d2b2..5ec4c07 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.h +++ b/Practise_18.11.20/Practise_18.11.20/Maze.h @@ -1,16 +1,29 @@ #pragma once -#include "MCell.h" #include +#include "MCell.h" +using namespace std; enum MazeElem { - PrevDown = 1 << 0, - PrevRight = 1 << 1, + Up = 1 << 0, + Left = 1 << 1, Down = 1 << 2, Right = 1 << 3, + UpLeft = Up + Left, + UpDown = Up + Down, + UpRight = Up + Right, + LeftDown = Left + Down, + LeftRight = Left + Right, + RightDown = Right + Down, + UpLeftDown = Up + Left + Down, + UpLeftRight = Up + Left + Right, + UpDownRight = Up + Down + Right, + LeftDownRight = Left + Down + Right, + AllDirection = Up + Left + Down + Right, }; class Maze { private: + map m_dict; MCell* m_field; int width; int size; From 6a71180116e64c42ad50e00c895f5ff053d227ab Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Mon, 7 Dec 2020 17:01:23 +0500 Subject: [PATCH 06/15] Added class MTreeNode; Improved the hasConnection method in Maze.cpp; A program has been written to display a new maze and tree. --- .../Practise_18.11.20/MTreeNode.cpp | 56 ++++++++++++++ .../Practise_18.11.20/MTreeNode.h | 25 +++++++ Practise_18.11.20/Practise_18.11.20/Maze.cpp | 6 ++ .../Practise_18.11.20/Practise_18.11.20.cpp | 74 ++++++++++++++++++- 4 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp create mode 100644 Practise_18.11.20/Practise_18.11.20/MTreeNode.h diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp new file mode 100644 index 0000000..b6db633 --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp @@ -0,0 +1,56 @@ +#include +#include +#include "MTreeNode.h" +using namespace std; + +MTreeNode MTreeNode::begintTree(int i, int j) { + MTreeNode node = MTreeNode(nullptr); + node.m_distance = i + j; + node.m_i = i; + node.m_j = j; + return node; +} + +MTreeNode::MTreeNode(MTreeNode* parent) { + m_parent = parent; + m_distance = 0; + m_i = 0; + m_j = 0; +} + +MTreeNode::~MTreeNode() { + for (auto& ptr : m_children) + delete ptr; +} + +int MTreeNode::i() const { return m_i; } + +int MTreeNode::j() const { return m_j; } + +const MTreeNode* MTreeNode::parent() const { return m_parent; } + +const MTreeNode* MTreeNode::child(int i) const { return m_children[i]; } + +int MTreeNode::distance() const { return m_distance; } + +int MTreeNode::childCount() const { return m_children.size(); } + +bool MTreeNode::addChild(int i, int j) { + if (abs(i - m_i + j - m_j) != 1) + return false; + m_children.push_back(new MTreeNode(this)); + m_children[m_children.size() - 1]->m_distance = i + j; + m_children[m_children.size() - 1]->m_i = i; + m_children[m_children.size() - 1]->m_j = j; + return true; +} + +MTreeNode* MTreeNode::hasChild(int i, int j) { + int count = m_children.size(); + for (int ind = 0; ind < count; ++ind) { + MTreeNode* child = m_children[ind]; + if (child->m_i == i && child->m_j == j) + return child; + } + return nullptr; +} \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h new file mode 100644 index 0000000..40fe5a1 --- /dev/null +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h @@ -0,0 +1,25 @@ +#pragma once +#include +using namespace std; + +class MTreeNode +{ +private: + int m_i, m_j; + int m_distance; + MTreeNode* m_parent; + vector m_children; + MTreeNode(MTreeNode* parent); +public: + ~MTreeNode(); + int i() const; + int j() const; + int distance() const; + int childCount() const; + bool addChild(int i, int j); + const MTreeNode* parent() const; + MTreeNode* hasChild(int i, int j); + const MTreeNode* child(int i) const; + static MTreeNode begintTree(int i, int j); +}; + diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.cpp b/Practise_18.11.20/Practise_18.11.20/Maze.cpp index ec81ae1..3596e3e 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Maze.cpp @@ -20,6 +20,8 @@ const MCell& Maze::cell(int i, int j) const { return m_field[i * width + j]; } bool Maze::hasConnection(int i1, int j1, int i2, int j2) { int ind1 = min(i1, i2); int ind2 = min(j1, j2); + if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= width) + return false; if (i1 == i2) { return m_field[ind1 * width + ind2].right(); } @@ -33,6 +35,8 @@ bool Maze::hasConnection(int i1, int j1, int i2, int j2) { bool Maze::makeConnection(int i1, int j1, int i2, int j2) { int ind1 = min(i1, i2); int ind2 = min(j1, j2); + if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= width) + return false; if (i1 == i2) { m_field[ind1 * width + ind2].m_right = true; return true; @@ -48,6 +52,8 @@ bool Maze::makeConnection(int i1, int j1, int i2, int j2) { bool Maze::removeConnection(int i1, int j1, int i2, int j2) { int ind1 = min(i1, i2); int ind2 = min(j1, j2); + if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= width) + return false; if (i1 == i2) { m_field[ind1 * width + ind2].m_right = false; return true; diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp index 898efd9..a7efdd9 100644 --- a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp @@ -1,12 +1,64 @@ #include +#include +#include +#include +#include "MTreeNode.h" #include "MCell.h" #include "Maze.h" using namespace std; +void BuildWebStepMaze(Maze& maze, const int& maze_size) { + int ind_right = 0; + int ind_down = 0; + for (int i = 0; i < maze_size; ++i) { + int count = i + 2; + for (int j = 0; j < maze_size; ++j) { + if (ind_right == j) { + ind_right++; + maze.makeConnection(i, j, i, j + 1); + } + if (ind_down == i && j < count) { + maze.makeConnection(i, j, i + 1, j); + } + } + ind_down++; + ind_right = ind_down; + } +} + +void GetTree(Maze& maze, const int& maze_size, MTreeNode& start_node) { + vector strings; + queue nodes; + MTreeNode* node = &start_node; + while (true) { + int i = node->i(); + int j = node->j(); + if (maze.hasConnection(i, j, i + 1, j)) { + node->addChild(i + 1, j); + nodes.push(node->hasChild(i + 1, j)); + } + if (maze.hasConnection(i, j, i, j + 1)) { + node->addChild(i, j + 1); + nodes.push(node->hasChild(i, j + 1)); + } + if (i < strings.size()) + strings[i] += to_string(node->distance()) + ' '; + else + strings.push_back(to_string(node->distance()) + ' '); + + if (nodes.size() == 0) + break; + node = nodes.front(); + nodes.pop(); + } + for (auto& str : strings) + cout << str << endl; +} + int main() { const int maze_size = 5; - Maze maze = Maze(maze_size, maze_size); + Maze maze1 = Maze(maze_size, maze_size); int ind_right = 0; int ind_down = 1; @@ -16,15 +68,29 @@ int main() break; } if (i == ind_right && j == ind_right) { - maze.makeConnection(i, j, i, j + 1); + maze1.makeConnection(i, j, i, j + 1); } if (i == ind_right && j == ind_down) { - maze.makeConnection(i, j, i + 1, j); + maze1.makeConnection(i, j, i + 1, j); } } ++ind_down; ++ind_right; } - maze.printMaze(); + cout << "Maze #1:" << endl << endl; + maze1.printMaze(); + + Maze maze2 = Maze(maze_size, maze_size); + BuildWebStepMaze(maze2, maze_size); + + cout << endl << "Maze #2:" << endl << endl; + maze2.printMaze(); + + MTreeNode start_node = MTreeNode::begintTree(0, 0); + + cout << endl << "TreeNode for Maze #2:" << endl << endl; + GetTree(maze2, maze_size, start_node); + + return 0; } From 5a49c521e47430a54970efa0dad6ddedf6cf12f4 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 12 Dec 2020 12:19:22 +0500 Subject: [PATCH 07/15] =?UTF-8?q?=D0=92=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D1=8B=D1=85=20=D0=B2=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=B0=D1=85=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B8=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Practise_18.11.20/MTreeNode.cpp | 15 ++-- .../Practise_18.11.20/MTreeNode.h | 2 +- Practise_18.11.20/Practise_18.11.20/Maze.cpp | 29 +++++++- Practise_18.11.20/Practise_18.11.20/Maze.h | 18 ----- .../Practise_18.11.20/Practise_18.11.20.cpp | 73 ++++++++++--------- 5 files changed, 74 insertions(+), 63 deletions(-) diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp index b6db633..1752c39 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp @@ -1,13 +1,14 @@ #include #include +#include #include "MTreeNode.h" using namespace std; -MTreeNode MTreeNode::begintTree(int i, int j) { - MTreeNode node = MTreeNode(nullptr); - node.m_distance = i + j; - node.m_i = i; - node.m_j = j; +MTreeNode* MTreeNode::begintTree(int i, int j) { + MTreeNode* node = new MTreeNode(nullptr); + node->m_distance = 0; + node->m_i = i; + node->m_j = j; return node; } @@ -39,7 +40,7 @@ bool MTreeNode::addChild(int i, int j) { if (abs(i - m_i + j - m_j) != 1) return false; m_children.push_back(new MTreeNode(this)); - m_children[m_children.size() - 1]->m_distance = i + j; + m_children[m_children.size() - 1]->m_distance = this->m_distance + 1; m_children[m_children.size() - 1]->m_i = i; m_children[m_children.size() - 1]->m_j = j; return true; @@ -53,4 +54,4 @@ MTreeNode* MTreeNode::hasChild(int i, int j) { return child; } return nullptr; -} \ No newline at end of file +} diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h index 40fe5a1..cfdf4d4 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h @@ -20,6 +20,6 @@ class MTreeNode const MTreeNode* parent() const; MTreeNode* hasChild(int i, int j); const MTreeNode* child(int i) const; - static MTreeNode begintTree(int i, int j); + static MTreeNode* begintTree(int i, int j); }; diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.cpp b/Practise_18.11.20/Practise_18.11.20/Maze.cpp index 3596e3e..33b194e 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Maze.cpp @@ -3,6 +3,24 @@ #include "Maze.h" using namespace std; +enum MazeElem { + Up = 1 << 0, + Left = 1 << 1, + Down = 1 << 2, + Right = 1 << 3, + UpLeft = Up + Left, + UpDown = Up + Down, + UpRight = Up + Right, + LeftDown = Left + Down, + LeftRight = Left + Right, + RightDown = Right + Down, + UpLeftDown = Up + Left + Down, + UpLeftRight = Up + Left + Right, + UpDownRight = Up + Down + Right, + LeftDownRight = Left + Down + Right, + AllDirection = Up + Left + Down + Right, +}; + Maze::Maze(int n, int m) { width = m; size = n * m; @@ -20,7 +38,7 @@ const MCell& Maze::cell(int i, int j) const { return m_field[i * width + j]; } bool Maze::hasConnection(int i1, int j1, int i2, int j2) { int ind1 = min(i1, i2); int ind2 = min(j1, j2); - if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= width) + if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= size - width) return false; if (i1 == i2) { return m_field[ind1 * width + ind2].right(); @@ -35,7 +53,7 @@ bool Maze::hasConnection(int i1, int j1, int i2, int j2) { bool Maze::makeConnection(int i1, int j1, int i2, int j2) { int ind1 = min(i1, i2); int ind2 = min(j1, j2); - if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= width) + if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= size - width) return false; if (i1 == i2) { m_field[ind1 * width + ind2].m_right = true; @@ -52,7 +70,7 @@ bool Maze::makeConnection(int i1, int j1, int i2, int j2) { bool Maze::removeConnection(int i1, int j1, int i2, int j2) { int ind1 = min(i1, i2); int ind2 = min(j1, j2); - if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= width) + if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= size - width) return false; if (i1 == i2) { m_field[ind1 * width + ind2].m_right = false; @@ -70,7 +88,10 @@ void Maze::printMaze() const { for (int n = 0; n < size; ++n) { int i = n / width; int j = n % width; - int res = m_field[n].right() << 3 | m_field[n].down() << 2; + int temp1 = m_field[n].down() << 2; + int temp2 = m_field[n].right() << 3; + int temp3 = temp2 | temp1; + int res = temp1 | temp2; if (i - 1 >= 0) res += m_field[(i - 1) * width + j].down() << 0; if (j - 1 >= 0) diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.h b/Practise_18.11.20/Practise_18.11.20/Maze.h index 5ec4c07..0c5f2e5 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.h +++ b/Practise_18.11.20/Practise_18.11.20/Maze.h @@ -3,24 +3,6 @@ #include "MCell.h" using namespace std; -enum MazeElem { - Up = 1 << 0, - Left = 1 << 1, - Down = 1 << 2, - Right = 1 << 3, - UpLeft = Up + Left, - UpDown = Up + Down, - UpRight = Up + Right, - LeftDown = Left + Down, - LeftRight = Left + Right, - RightDown = Right + Down, - UpLeftDown = Up + Left + Down, - UpLeftRight = Up + Left + Right, - UpDownRight = Up + Down + Right, - LeftDownRight = Left + Down + Right, - AllDirection = Up + Left + Down + Right, -}; - class Maze { private: map m_dict; diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp index a7efdd9..a71e2f2 100644 --- a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp @@ -1,16 +1,23 @@ -#include +#include +#include #include -#include +#include #include #include "MTreeNode.h" #include "MCell.h" #include "Maze.h" +#include using namespace std; -void BuildWebStepMaze(Maze& maze, const int& maze_size) { +void BuildMaze2(Maze& maze, const int& maze_size); +void BuildMaze1(Maze& maze, const int& maze_size); +void GetTreeForMaze2(Maze& maze, const int& maze_size, MTreeNode* node); + + +void BuildMaze2(Maze& maze, const int& maze_size) { int ind_right = 0; int ind_down = 0; - for (int i = 0; i < maze_size; ++i) { + for (int i = 0; i < maze_size - 1; ++i) { int count = i + 2; for (int j = 0; j < maze_size; ++j) { if (ind_right == j) { @@ -26,10 +33,29 @@ void BuildWebStepMaze(Maze& maze, const int& maze_size) { } } -void GetTree(Maze& maze, const int& maze_size, MTreeNode& start_node) { +void BuildMaze1(Maze& maze, const int& maze_size) { + int ind_right = 0; + int ind_down = 1; + for (int i = 0; i < maze_size; ++i) { + for (int j = 0; j < maze_size; ++j) { + if (i == maze_size - 1 && j == maze_size - 1) { + break; + } + if (i == ind_right && j == ind_right) { + maze.makeConnection(i, j, i, j + 1); + } + if (i == ind_right && j == ind_down) { + maze.makeConnection(i, j, i + 1, j); + } + } + ++ind_down; + ++ind_right; + } +} + +void GetTreeForMaze2(Maze& maze, const int& maze_size, MTreeNode* node) { vector strings; queue nodes; - MTreeNode* node = &start_node; while (true) { int i = node->i(); int j = node->j(); @@ -59,38 +85,19 @@ int main() { const int maze_size = 5; Maze maze1 = Maze(maze_size, maze_size); - - int ind_right = 0; - int ind_down = 1; - for (int i = 0; i < maze_size; ++i) { - for (int j = 0; j < maze_size; ++j) { - if (i == maze_size - 1 && j == maze_size - 1) { - break; - } - if (i == ind_right && j == ind_right) { - maze1.makeConnection(i, j, i, j + 1); - } - if (i == ind_right && j == ind_down) { - maze1.makeConnection(i, j, i + 1, j); - } - } - ++ind_down; - ++ind_right; - } - + BuildMaze1(maze1, maze_size); //Построение простого лабиринта-лесенки cout << "Maze #1:" << endl << endl; - maze1.printMaze(); + maze1.printMaze(); //Печать лабиринта-лесенки Maze maze2 = Maze(maze_size, maze_size); - BuildWebStepMaze(maze2, maze_size); - + BuildMaze2(maze2, maze_size); //Построение лабиринта паутинки-лесенки cout << endl << "Maze #2:" << endl << endl; - maze2.printMaze(); - - MTreeNode start_node = MTreeNode::begintTree(0, 0); + maze2.printMaze(); //Печать лабиринта паутинки-лесенки - cout << endl << "TreeNode for Maze #2:" << endl << endl; - GetTree(maze2, maze_size, start_node); + MTreeNode* start_node = MTreeNode::begintTree(0, 0); + cout << endl << "Node tree for Maze #2:" << endl << endl; + GetTreeForMaze2(maze2, maze_size, start_node); //Построение и печать дерева обхода для лабиринта паутинки-лесенки + delete start_node; return 0; } From 26cb0ff53add000c7f4516fb7916971b388683ef Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 12 Dec 2020 12:51:19 +0500 Subject: [PATCH 08/15] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=20searchNode=20=D0=B4=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=B2=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=20MTreeNode.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Practise_18.11.20/MTreeNode.cpp | 34 +++++++++++++++++++ .../Practise_18.11.20/MTreeNode.h | 1 + 2 files changed, 35 insertions(+) diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp index 1752c39..61a3cb5 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp @@ -55,3 +55,37 @@ MTreeNode* MTreeNode::hasChild(int i, int j) { } return nullptr; } + +MTreeNode* MTreeNode::searchNode(const MTreeNode& tree, const int i, const int j) { + queue nodes; + MTreeNode* parent = tree.m_parent; + if (parent) { + if (parent->i() == i && parent->j() == j) + return parent; + while (true) { + MTreeNode* temp = parent->m_parent; + if (!temp) break; + parent = temp; + } + while (true) + { + for (const auto child : parent->m_children) { + nodes.push(child); + if (child->i() == i && child->j() == j) + return child; + } + if (nodes.size() == 0) + break; + parent = nodes.front(); + nodes.pop(); + } + } + else { + for (const auto child : tree.m_children) { + nodes.push(child); + if (child->i() == i && child->j() == j) + return child; + } + } + return nullptr; +} \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h index cfdf4d4..2370bf6 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h @@ -21,5 +21,6 @@ class MTreeNode MTreeNode* hasChild(int i, int j); const MTreeNode* child(int i) const; static MTreeNode* begintTree(int i, int j); + static MTreeNode* searchNode(const MTreeNode& tree, const int i, const int j); }; From a442304136cfca463d3aade32fc7801a74838ac2 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 12 Dec 2020 12:53:38 +0500 Subject: [PATCH 09/15] =?UTF-8?q?=D0=92=20Practise=5F18.11.20.cpp=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20task=2010.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Practise_18.11.20/Practise_18.11.20.cpp | 109 +++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp index a71e2f2..46cf28c 100644 --- a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp @@ -9,10 +9,103 @@ #include using namespace std; +void buildFullMaze(Maze& iMaze, const MTreeNode* tree); void BuildMaze2(Maze& maze, const int& maze_size); void BuildMaze1(Maze& maze, const int& maze_size); +void GetTree(MTreeNode* node, const int& width, const int& height); void GetTreeForMaze2(Maze& maze, const int& maze_size, MTreeNode* node); +void PrintTreeInfo(const MTreeNode* tree, const int& width, const int& height); +vector GetRandCoordinates(const int& width, const int& height); +void PrintTreeInfo(const MTreeNode* tree, const int& width, const int& height) { + int sum_weights = 0; + int max_weight = 0; + queue nodes; + vector> to_print(height); + for (auto& vctr : to_print) + vctr = vector(width); + while (true) { + int i = tree->i(); + int j = tree->j(); + sum_weights += tree->distance(); + max_weight = tree->distance() > max_weight ? tree->distance() : max_weight; + to_print[i][j] = to_string(tree->distance()); + for (int ind = 0; ind < tree->childCount(); ++ind) + nodes.push(tree->child(ind)); + if (nodes.size() == 0) + break; + tree = nodes.front(); + nodes.pop(); + } + for (auto& vctr : to_print) { + for (auto& str : vctr) + cout << setw(5) << str; + cout << endl; + } + int average_weight = sum_weights / (1. * width * height); + cout << endl << "The maximum weight of tree vertices: " << max_weight << endl; + cout << endl << "The average weight of the vertices: " << average_weight << endl; +} + +void buildFullMaze(Maze& iMaze, const MTreeNode* tree) { + queue nodes; + while (true) { + int t_i = tree->i(); + int t_j = tree->j(); + for (int ind = 0; ind < tree->childCount(); ++ind) { + const MTreeNode* child = tree->child(ind); + iMaze.makeConnection(t_i, t_j, child->i(), child->j()); + nodes.push(child); + } + if (nodes.size() == 0) + break; + tree = nodes.front(); + nodes.pop(); + } +} + +vector GetRandCoordinates(const int& width, const int& height) { + if (width == 0 || height == 0) + return { 0, 0 }; + srand(time(NULL)); + int flag = rand() % 4 + 1; + switch (flag) { + case(1): + return { 0, rand() % width }; + case(2): + return { rand() % height, 0 }; + case(3): + return { height - 1, rand() % width }; + default: + return { rand() % height, width - 1 }; + } +} + +void GetTree(MTreeNode* tree, const int& width, const int& height) { + queue nodes; + while (true) { + int n_i = tree->i(); + int n_j = tree->j(); + for (int i = -1; i < 2; ++i) + for (int j = -1; j < 2; ++j) { + if ((i != 0 && j != 0) || (i == 0 && j == 0)) + continue; + if (n_i + i < 0 || n_i + i >= height || n_j + j < 0 || n_j + j >= width) + continue; + int child_i = n_i + i; + int child_j = n_j + j; + if (!MTreeNode::searchNode(*tree, child_i, child_j)) { + tree->addChild(child_i, child_j); + MTreeNode* child = tree->hasChild(child_i, child_j); + nodes.push(child); + } + } + if (nodes.size() == 0) + break; + tree = nodes.front(); + nodes.pop(); + } +} void BuildMaze2(Maze& maze, const int& maze_size) { int ind_right = 0; @@ -71,7 +164,7 @@ void GetTreeForMaze2(Maze& maze, const int& maze_size, MTreeNode* node) { strings[i] += to_string(node->distance()) + ' '; else strings.push_back(to_string(node->distance()) + ' '); - + if (nodes.size() == 0) break; node = nodes.front(); @@ -99,5 +192,19 @@ int main() GetTreeForMaze2(maze2, maze_size, start_node); //Построение и печать дерева обхода для лабиринта паутинки-лесенки delete start_node; + int width, height; + cout << endl << "Enter the height and width of the maze separated by a space: "; + cin >> height >> width; //Считывание размеров случайного лабиринта + + auto start_coordinates = GetRandCoordinates(width, height); + MTreeNode* tree = MTreeNode::begintTree(start_coordinates[0], start_coordinates[1]); + GetTree(tree, width, height); //Построение дерева для случайного лабиринта заданного размера + Maze maze3 = Maze(height, width); + buildFullMaze(maze3, tree); //Создание лабиринта по дереву + cout << endl << "Maze #3:" << endl << endl; + maze3.printMaze(); //Печать случайного лабиринта заданного размера + cout << endl << "Node tree for random maze:" << endl << endl; + PrintTreeInfo(tree, width, height); //Печать весов дерева на экран построчно и пеать информации о вершинах + return 0; } From b3a3245b81f3e2c0bf4ef37dfe3b7c658404dc1e Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 26 Dec 2020 13:18:32 +0500 Subject: [PATCH 10/15] m_distance = 0; node->m_i = i; node->m_j = j; return node; @@ -14,31 +13,21 @@ MTreeNode* MTreeNode::begintTree(int i, int j) { MTreeNode::MTreeNode(MTreeNode* parent) { m_parent = parent; - m_distance = 0; - m_i = 0; - m_j = 0; + m_distance = parent == nullptr ? 0 : parent->m_distance; } MTreeNode::~MTreeNode() { - for (auto& ptr : m_children) + for (auto ptr : m_children) delete ptr; } -int MTreeNode::i() const { return m_i; } - -int MTreeNode::j() const { return m_j; } - -const MTreeNode* MTreeNode::parent() const { return m_parent; } - -const MTreeNode* MTreeNode::child(int i) const { return m_children[i]; } - -int MTreeNode::distance() const { return m_distance; } - -int MTreeNode::childCount() const { return m_children.size(); } - bool MTreeNode::addChild(int i, int j) { if (abs(i - m_i + j - m_j) != 1) return false; + + for (auto &child : m_children) + if (child->m_i == i && child->m_j == j) + return false; m_children.push_back(new MTreeNode(this)); m_children[m_children.size() - 1]->m_distance = this->m_distance + 1; m_children[m_children.size() - 1]->m_i = i; @@ -46,46 +35,44 @@ bool MTreeNode::addChild(int i, int j) { return true; } -MTreeNode* MTreeNode::hasChild(int i, int j) { - int count = m_children.size(); - for (int ind = 0; ind < count; ++ind) { - MTreeNode* child = m_children[ind]; +MTreeNode* MTreeNode::hasChild(int i, int j) const{ + const int count = m_children.size(); + for (auto child : m_children){ if (child->m_i == i && child->m_j == j) return child; } return nullptr; } -MTreeNode* MTreeNode::searchNode(const MTreeNode& tree, const int i, const int j) { +const MTreeNode* MTreeNode::searchNode(const int i, const int j) { + queue nodes; + auto node = searchRoot(this); + while (true) + { + if (node->m_i == i && node->m_j == j) return node; + for (const auto child : node->m_children) { + if (child->i() == i && child->j() == j) + return child; + nodes.push(child); + } + if (nodes.size() == 0) + break; + node = nodes.front(); + nodes.pop(); + } + return nullptr; +} + +const MTreeNode* MTreeNode::searchRoot(const MTreeNode* node) { queue nodes; - MTreeNode* parent = tree.m_parent; + MTreeNode* parent = node->m_parent; if (parent) { - if (parent->i() == i && parent->j() == j) - return parent; while (true) { MTreeNode* temp = parent->m_parent; if (!temp) break; parent = temp; } - while (true) - { - for (const auto child : parent->m_children) { - nodes.push(child); - if (child->i() == i && child->j() == j) - return child; - } - if (nodes.size() == 0) - break; - parent = nodes.front(); - nodes.pop(); - } + return parent; } - else { - for (const auto child : tree.m_children) { - nodes.push(child); - if (child->i() == i && child->j() == j) - return child; - } - } - return nullptr; + else return node; } \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h index 2370bf6..e1fc5d7 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h @@ -4,23 +4,24 @@ using namespace std; class MTreeNode { -private: - int m_i, m_j; - int m_distance; - MTreeNode* m_parent; - vector m_children; - MTreeNode(MTreeNode* parent); public: ~MTreeNode(); - int i() const; - int j() const; - int distance() const; - int childCount() const; + int i() const { return m_i; } + int j() const { return m_j; } + int distance() const { return m_distance; } + int childCount() const { return m_children.size(); } bool addChild(int i, int j); - const MTreeNode* parent() const; - MTreeNode* hasChild(int i, int j); - const MTreeNode* child(int i) const; + const MTreeNode* parent() const { return m_parent; } + MTreeNode* hasChild(int i, int j) const; + const MTreeNode* child(int i) const { return m_children[i]; } static MTreeNode* begintTree(int i, int j); - static MTreeNode* searchNode(const MTreeNode& tree, const int i, const int j); + const MTreeNode* searchNode(const int i, const int j); + const MTreeNode* searchRoot(const MTreeNode* node); +private: + int m_i, m_j = 0; + int m_distance = 0; + MTreeNode* m_parent = nullptr; + vector m_children; + MTreeNode(MTreeNode* parent); }; diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.cpp b/Practise_18.11.20/Practise_18.11.20/Maze.cpp index 33b194e..ebce943 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Maze.cpp @@ -1,6 +1,8 @@ #include #include +#include #include "Maze.h" +#include using namespace std; enum MazeElem { @@ -20,47 +22,54 @@ enum MazeElem { LeftDownRight = Left + Down + Right, AllDirection = Up + Left + Down + Right, }; - -Maze::Maze(int n, int m) { - width = m; - size = n * m; - m_field = new MCell[size]; - m_dict = { {AllDirection, char(197)}, {UpLeftDown, char(180)}, {UpLeftRight, char(193)}, +static map m_dict = { {AllDirection, char(197)}, {UpLeftDown, char(180)}, {UpLeftRight, char(193)}, {UpDownRight, char(195)}, {LeftDownRight, char(194)}, {UpLeft, char(217)}, {UpDown, char(179)}, {UpRight, char(192)}, {LeftDown, char(191)}, - {LeftRight, char(196)}, {RightDown, char(218)}, {Up, '0'}, {Down, '0'}, {Left, '0'}, {Right, '0'} }; + {LeftRight, char(196)}, {RightDown, char(218)}, {Up, '0'}, {Down, '0'}, {Left, '0'}, {Right, '0'}, {0, '0'} +}; + +Maze::Maze(int n, int m) { + m_width = m; + m_height = n; + m_field = new MCell[m * n]; } Maze::~Maze() { delete[] m_field; } -const MCell& Maze::cell(int i, int j) const { return m_field[i * width + j]; } +const MCell& Maze::cell(int i, int j) const { + assert(i >= 0); + assert(j >= 0); + return m_field[i * m_width + j]; +} -bool Maze::hasConnection(int i1, int j1, int i2, int j2) { - int ind1 = min(i1, i2); - int ind2 = min(j1, j2); - if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= size - width) +bool Maze::hasConnection(int i1, int j1, int i2, int j2) const { + if ((abs(i1 - i2) + abs(j1 - j2) != 1)) return false; + if (j2 < 0 || j2 >= m_width || i2 < 0 || i2 >= m_height) return false; + const int ind1 = min(i1, i2); + const int ind2 = min(j1, j2); if (i1 == i2) { - return m_field[ind1 * width + ind2].right(); + return m_field[ind1 * m_width + ind2].right(); } else if (j2 == j1) { - return m_field[ind1 * width + ind2].down(); + return m_field[ind1 * m_width + ind2].down(); } else return false; } bool Maze::makeConnection(int i1, int j1, int i2, int j2) { + if ((abs(i1 - i2) + abs(j1 - j2) != 1)) return false; + if (j2 < 0 || j2 >= m_width || i2 < 0 || i2 >= m_height) + return false; int ind1 = min(i1, i2); int ind2 = min(j1, j2); - if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= size - width) - return false; if (i1 == i2) { - m_field[ind1 * width + ind2].m_right = true; + m_field[ind1 * m_width + ind2].m_right = true; return true; } else if (j1 == j2) { - m_field[ind1 * width + ind2].m_down = true; + m_field[ind1 * m_width + ind2].m_down = true; return true; } else @@ -68,41 +77,39 @@ bool Maze::makeConnection(int i1, int j1, int i2, int j2) { } bool Maze::removeConnection(int i1, int j1, int i2, int j2) { + if ((abs(i1 - i2) + abs(j1 - j2) != 1)) return false; + if (j2 < 0 || j2 >= m_width || i2 < 0 || i2 >= m_height) + return false; int ind1 = min(i1, i2); int ind2 = min(j1, j2); - if (j2 < 0 || j2 >= width || i2 < 0 || i2 >= size - width) - return false; if (i1 == i2) { - m_field[ind1 * width + ind2].m_right = false; - return true; + bool res = m_field[ind1 * m_width + ind2].m_right; + m_field[ind1 * m_width + ind2].m_right = false; + return res; } else if (j2 == j1) { - m_field[ind1 * width + ind2].m_down = false; - return true; + bool res = m_field[ind1 * m_width + ind2].m_down; + m_field[ind1 * m_width + ind2].m_down = false; + return res; } else return false; } void Maze::printMaze() const { - for (int n = 0; n < size; ++n) { - int i = n / width; - int j = n % width; - int temp1 = m_field[n].down() << 2; - int temp2 = m_field[n].right() << 3; - int temp3 = temp2 | temp1; - int res = temp1 | temp2; + for (int n = 0; n < m_width * m_height; ++n) { + int i = n / m_width; + int j = n % m_width; + int res = m_field[n].down() << 2 | m_field[n].right() << 3; if (i - 1 >= 0) - res += m_field[(i - 1) * width + j].down() << 0; + res += m_field[(i - 1) * m_width + j].down() << 0; if (j - 1 >= 0) - res += m_field[i * width + j - 1].right() << 1; + res += m_field[i * m_width + j - 1].right() << 1; - if (res == 0 || res == 1 || res == 2 || res == 4 || res == 8) - cout << '0'; - else - cout << m_dict.find(res)->second; + assert(typeid(m_dict.find(res)->second) == typeid(char)); + cout << m_dict.find(res)->second; - if (j + 1 == width) + if (j + 1 == m_width) cout << endl; } } \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.h b/Practise_18.11.20/Practise_18.11.20/Maze.h index 0c5f2e5..7eb945c 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.h +++ b/Practise_18.11.20/Practise_18.11.20/Maze.h @@ -1,20 +1,18 @@ #pragma once -#include +//#include #include "MCell.h" -using namespace std; class Maze { -private: - map m_dict; - MCell* m_field; - int width; - int size; public: Maze(int n, int m); ~Maze(); const MCell& cell(int i, int j) const; - bool hasConnection(int i1, int j1, int i2, int j2); + bool hasConnection(int i1, int j1, int i2, int j2) const; bool makeConnection(int i1, int j1, int i2, int j2); bool removeConnection(int i1, int j1, int i2, int j2); void printMaze() const; +private: + MCell* m_field = nullptr; + int m_width = 0; + int m_height = 0; }; diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp index 46cf28c..96dac3f 100644 --- a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp @@ -10,39 +10,39 @@ using namespace std; void buildFullMaze(Maze& iMaze, const MTreeNode* tree); -void BuildMaze2(Maze& maze, const int& maze_size); -void BuildMaze1(Maze& maze, const int& maze_size); -void GetTree(MTreeNode* node, const int& width, const int& height); -void GetTreeForMaze2(Maze& maze, const int& maze_size, MTreeNode* node); -void PrintTreeInfo(const MTreeNode* tree, const int& width, const int& height); -vector GetRandCoordinates(const int& width, const int& height); +void BuildMaze2(Maze& maze, const int maze_size); +void BuildMaze1(Maze& maze, const int maze_size); +void BuildTree(MTreeNode* node, const int width, const int height); +void BuildTreeForMaze2(Maze& maze, const int maze_size, MTreeNode* node); +void PrintTreeInfo(const MTreeNode* tree, const int width, const int height); +vector GetRandCoordinates(const int width, const int height); -void PrintTreeInfo(const MTreeNode* tree, const int& width, const int& height) { +void PrintTreeInfo(const MTreeNode* tree, const int width, const int height) { int sum_weights = 0; int max_weight = 0; queue nodes; vector> to_print(height); for (auto& vctr : to_print) vctr = vector(width); - while (true) { + for (int p = 0; p < width * height; ++p) { int i = tree->i(); int j = tree->j(); sum_weights += tree->distance(); - max_weight = tree->distance() > max_weight ? tree->distance() : max_weight; + max_weight = max(tree->distance(), max_weight); to_print[i][j] = to_string(tree->distance()); for (int ind = 0; ind < tree->childCount(); ++ind) nodes.push(tree->child(ind)); - if (nodes.size() == 0) - break; + if (p + 1 == width * height) break; tree = nodes.front(); nodes.pop(); } + const int max_len = size(to_string(max_weight)); for (auto& vctr : to_print) { for (auto& str : vctr) - cout << setw(5) << str; + cout << setw(max_len + 1) << str; cout << endl; } - int average_weight = sum_weights / (1. * width * height); + const int average_weight = sum_weights / (1. * width * height); cout << endl << "The maximum weight of tree vertices: " << max_weight << endl; cout << endl << "The average weight of the vertices: " << average_weight << endl; } @@ -64,7 +64,7 @@ void buildFullMaze(Maze& iMaze, const MTreeNode* tree) { } } -vector GetRandCoordinates(const int& width, const int& height) { +vector GetRandCoordinates(const int width, const int height) { if (width == 0 || height == 0) return { 0, 0 }; srand(time(NULL)); @@ -81,7 +81,7 @@ vector GetRandCoordinates(const int& width, const int& height) { } } -void GetTree(MTreeNode* tree, const int& width, const int& height) { +void BuildTree(MTreeNode* tree, const int width, const int height) { queue nodes; while (true) { int n_i = tree->i(); @@ -94,7 +94,7 @@ void GetTree(MTreeNode* tree, const int& width, const int& height) { continue; int child_i = n_i + i; int child_j = n_j + j; - if (!MTreeNode::searchNode(*tree, child_i, child_j)) { + if (!tree->searchNode(child_i, child_j)) { tree->addChild(child_i, child_j); MTreeNode* child = tree->hasChild(child_i, child_j); nodes.push(child); @@ -107,7 +107,7 @@ void GetTree(MTreeNode* tree, const int& width, const int& height) { } } -void BuildMaze2(Maze& maze, const int& maze_size) { +void BuildMaze2(Maze& maze, const int maze_size) { int ind_right = 0; int ind_down = 0; for (int i = 0; i < maze_size - 1; ++i) { @@ -126,7 +126,7 @@ void BuildMaze2(Maze& maze, const int& maze_size) { } } -void BuildMaze1(Maze& maze, const int& maze_size) { +void BuildMaze1(Maze& maze, const int maze_size) { int ind_right = 0; int ind_down = 1; for (int i = 0; i < maze_size; ++i) { @@ -146,7 +146,7 @@ void BuildMaze1(Maze& maze, const int& maze_size) { } } -void GetTreeForMaze2(Maze& maze, const int& maze_size, MTreeNode* node) { +void BuildTreeForMaze2(Maze& maze, const int maze_size, MTreeNode* node) { vector strings; queue nodes; while (true) { @@ -189,16 +189,16 @@ int main() MTreeNode* start_node = MTreeNode::begintTree(0, 0); cout << endl << "Node tree for Maze #2:" << endl << endl; - GetTreeForMaze2(maze2, maze_size, start_node); //Построение и печать дерева обхода для лабиринта паутинки-лесенки + BuildTreeForMaze2(maze2, maze_size, start_node); //Построение и печать дерева обхода для лабиринта паутинки-лесенки delete start_node; int width, height; cout << endl << "Enter the height and width of the maze separated by a space: "; cin >> height >> width; //Считывание размеров случайного лабиринта - auto start_coordinates = GetRandCoordinates(width, height); + auto start_coordinates = GetRandCoordinates(width, height);//Функция для получения произвольных координат на границе лабиринта MTreeNode* tree = MTreeNode::begintTree(start_coordinates[0], start_coordinates[1]); - GetTree(tree, width, height); //Построение дерева для случайного лабиринта заданного размера + BuildTree(tree, width, height); //Построение дерева для случайного лабиринта заданного размера Maze maze3 = Maze(height, width); buildFullMaze(maze3, tree); //Создание лабиринта по дереву cout << endl << "Maze #3:" << endl << endl; From 3bd31b1f37eeee67cdf26952a3ad2e2de27f7834 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 26 Dec 2020 13:24:56 +0500 Subject: [PATCH 11/15] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8;=20=D0=9E=D0=B4=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=BE=D1=87=D0=BD=D1=8B=D0=B5=20=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=B2=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=BE=D1=87=D0=BD=D1=8B=D1=85=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= =?UTF-8?q?=D1=85;=20=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=B8=D0=B7=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BE=D1=87=D0=BD?= =?UTF-8?q?=D0=B8=D0=BA=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp index c62ec66..dd9d389 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp @@ -13,7 +13,7 @@ MTreeNode* MTreeNode::begintTree(int i, int j) { MTreeNode::MTreeNode(MTreeNode* parent) { m_parent = parent; - m_distance = parent == nullptr ? 0 : parent->m_distance; + m_distance = parent == nullptr ? 0 : parent->m_distance + 1; } MTreeNode::~MTreeNode() { From 2af4347430e07cce7483c482543a05be76ec2547 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 26 Dec 2020 13:27:58 +0500 Subject: [PATCH 12/15] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8;=20=D0=9E=D0=B4=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=BE=D0=BD=D1=8B=D0=B5=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=D1=8B=20=D0=BF=D1=80=D0=BE=D0=B8=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=20=D0=B2=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D1=8B=D1=85=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=B0=D1=85;=20=D0=92=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D1=8B=D1=85=20=D0=B1=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D1=88=D0=B5=20=D0=BD=D0=B5=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D1=83=D1=8E=D1=82=D1=81=D1=8F=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=80=D0=B0=D0=BD=D1=81=D1=82=D0=B2=D0=B0=20?= =?UTF-8?q?=D0=B8=D0=BC=D0=B5=D0=BD;=20const=20=D0=B5=D1=81=D1=82=D1=8C=20?= =?UTF-8?q?=D0=B2=D0=B5=D0=B7=D0=B4=D0=B5,=20=D0=B3=D0=B4=D0=B5=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B6=D0=BD=D0=BE;=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BD=D0=B5=D0=BB=D0=B5=D0=BF=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20searchNode;=20=D0=94?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20=D0=BA?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D1=8F=20=D0=B2=20=D0=B4=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B2=D0=B5=20searchRoot;=20=D0=B2=D0=BE=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=D1=85=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BE=D1=87?= =?UTF-8?q?=D0=BD=D0=B8=D0=BA=D0=B0=D1=85=20=D1=81=D0=BD=D0=B0=D1=87=D0=B0?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=B8=D0=B4=D0=B5=D1=82=20public-=D1=81=D0=B5?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D1=8F;=20=D0=9F=D1=80=D0=B8=20=D0=B6=D0=B8?= =?UTF-8?q?=D0=B2=D0=BE=D0=BC=20=D1=80=D0=BE=D0=B4=D0=B8=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=20=D1=83=20=D0=B4=D0=BE=D1=87=D0=B5=D1=80=D0=BD=D0=B5?= =?UTF-8?q?=D0=B3=D0=BE=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20distance=20=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9;=20=D0=9B=D0=B8=D1=88=D0=BD=D0=B8=D0=B5=20=D1=81?= =?UTF-8?q?=D1=81=D1=8B=D0=BB=D0=BA=D0=B8=20=D0=BF=D1=80=D0=B8=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=85=D0=BE=D0=B6=D0=B4=D0=B5=D0=BD=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=20(=D0=B8=20=D0=BF=D1=80=D0=B8=20int)=20=D1=83=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D1=8B;=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B1=D0=B5=D0=BD=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B8=D1=81=D1=85=D0=BE=D0=B4=D0=B8=D1=82=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D0=BA=D0=B0=20=D0=BD=D0=B0=20=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B0=D0=BB=D0=B8=D1=87=D0=B8=D0=B5=20=D0=BA=20?= =?UTF-8?q?=D1=80=D0=BE=D0=B4=D0=B8=D1=82=D0=B5=D0=BB=D1=8F;=20m=5Fdict=20?= =?UTF-8?q?=D0=B8=D0=B7=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Maze=20?= =?UTF-8?q?=D0=B1=D0=BE=D0=BB=D1=8C=D1=89=D0=B5=20=D0=BD=D0=B5=20=D1=87?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D0=BE=D0=BD=20sctatic;=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20assert=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B5=20=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BA=D0=BB=D0=B5=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BB=D0=B0=D0=B1=D0=B8=D1=80=D0=B8=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=20=D0=B8=20=D0=B3=D0=B4=D0=B5-=D1=82=D0=BE=20=D0=B5?= =?UTF-8?q?=D1=89=D0=B5;=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=81=D0=BE=D1=81=D0=B5=D0=B4=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=BE=20=D0=B4=D0=B2=D1=83=D1=85=20=D0=BA=D0=BB=D0=B5?= =?UTF-8?q?=D1=82=D0=BE=D0=BA=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20Maze;=20=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C,=20=D0=B5=D1=81?= =?UTF-8?q?=D0=BB=D0=B8=20=D0=B7=D0=B2=D1=8F=D0=B7=D0=B8=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B1=D1=8B=D0=BB=D0=BE=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20rem?= =?UTF-8?q?oveConnection=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89?= =?UTF-8?q?=D0=B0=D0=B5=D1=82=20=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BD=D0=BE=D0=B5=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5;=20=D0=92=D0=BE=D0=BE=D0=B1=D1=89=D0=B5,=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=B6=D0=B5=D1=82=D1=81=D1=8F,=20=D0=B2=D1=81=D0=B5=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB,=20=D1=87=D1=82?= =?UTF-8?q?=D0=BE=20=D0=B1=D1=8B=D0=BB=D0=BE=20=D0=BF=D1=80=D0=BE=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=B0=D0=BD=D0=BE=20=D0=92=D0=B0=D0=BC=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Practise_18.11.20/Practise_18.11.20/MTreeNode.h | 3 +-- Practise_18.11.20/Practise_18.11.20/Maze.h | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h index e1fc5d7..a337b78 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h @@ -1,6 +1,5 @@ #pragma once #include -using namespace std; class MTreeNode { @@ -21,7 +20,7 @@ class MTreeNode int m_i, m_j = 0; int m_distance = 0; MTreeNode* m_parent = nullptr; - vector m_children; + std::vector m_children; MTreeNode(MTreeNode* parent); }; diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.h b/Practise_18.11.20/Practise_18.11.20/Maze.h index 7eb945c..1fcc379 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.h +++ b/Practise_18.11.20/Practise_18.11.20/Maze.h @@ -1,5 +1,4 @@ #pragma once -//#include #include "MCell.h" class Maze { From 59db137bba770d04b9b193a52fb1d0fe33efd306 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sun, 27 Dec 2020 01:27:31 +0500 Subject: [PATCH 13/15] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20MCell.cp?= =?UTF-8?q?p;=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B5=20MTreeNode=20=D1=83=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B7=D0=B4=D0=BD=D0=B8=D0=BB=20=D1=82=D0=B5=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=80=D0=BD=D1=8B=D0=B9=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80;=20=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B8=D0=BD=D0=B4=D0=B5=D0=BA=D1=81=D0=B0=D1=86=D0=B8=D1=8E=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=20=D0=B2=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B5=20addChild=20?= =?UTF-8?q?=D0=BD=D0=B0=20.back;=20=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20=D0=B2?= =?UTF-8?q?=D1=8B=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20m=5Fdi?= =?UTF-8?q?stance=20=D0=B8=D0=B7=20addChild;=20=D1=83=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B5=D0=B5=20=D0=B2=D1=8B?= =?UTF-8?q?=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=BB?= =?UTF-8?q?=D0=B8=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=20=D0=B4=D0=B5=D1=82=D0=B5=D0=B9=20=D0=B2=20hasChi?= =?UTF-8?q?ld;=20=D0=92=D0=B5=D1=80=D0=BD=D1=83=D0=BB=20=D1=81=D1=81=D1=8B?= =?UTF-8?q?=D0=BB=D0=BA=D1=83=20=D0=B4=D0=BB=D1=8F=20=D0=B8=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D1=83=20=D0=B2=20?= =?UTF-8?q?hasChild;=20=D0=9F=D1=80=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D0=B5?= =?UTF-8?q?=D0=B2=20=D0=B4=D0=BB=D1=8F=20serchNode,=20searchRoot;=20=D0=A3?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=BB=20return=20nullptr=20=D1=83=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20searchNode;=20=D0=A3=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D1=88=D0=B8=D0=BB=20=D0=B2=D0=BB=D0=BE=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=B5=D0=B9=20=D1=87=D0=B8=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D0=BC=D0=BE=D1=81=D1=82=D0=B8=20=D0=BA=D0=BE=D0=B4?= =?UTF-8?q?=D0=B0=20=D1=83=20searchRoot;=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20assert=20=D1=83=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=D0=B0=20cell=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Ma?= =?UTF-8?q?ze;=20=D0=92=D1=8B=D0=BD=D0=B5=D1=81=20=D0=B2=20=D0=BE=D1=82?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=BD=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=20checkConnection=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BA=D1=83=20=D0=BD=D0=B0=20=D0=B2=D1=88=D0=B8=D0=B2=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=D1=85=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B5=D0=B9;=20=D0=94=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=B2=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=20checkConnecion=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D0=BA=D1=83=20=D0=BD=D0=B5=D1=80=D0=B5=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D1=8F?= =?UTF-8?q?;=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B8=20=D0=BD=D1=83?= =?UTF-8?q?=D0=B6=D0=BD=D1=8B=D0=B5=20const;=20=D0=B4=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D1=83=D1=81=D1=82=D1=83=D1=8E=20=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D1=83=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=86?= =?UTF-8?q?=D0=B5=20Maze.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Practise_18.11.20/Practise_18.11.20/MCell.cpp | 5 --- .../Practise_18.11.20/MTreeNode.cpp | 18 +++++------ Practise_18.11.20/Practise_18.11.20/Maze.cpp | 32 +++++++++---------- Practise_18.11.20/Practise_18.11.20/Maze.h | 3 +- 4 files changed, 26 insertions(+), 32 deletions(-) delete mode 100644 Practise_18.11.20/Practise_18.11.20/MCell.cpp diff --git a/Practise_18.11.20/Practise_18.11.20/MCell.cpp b/Practise_18.11.20/Practise_18.11.20/MCell.cpp deleted file mode 100644 index 77bd43c..0000000 --- a/Practise_18.11.20/Practise_18.11.20/MCell.cpp +++ /dev/null @@ -1,5 +0,0 @@ -//#include "MCell.h" -// -//bool MCell::down() const { return m_down; } -// -//bool MCell::right() const { return m_right; } diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp index dd9d389..863fef8 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp @@ -13,7 +13,7 @@ MTreeNode* MTreeNode::begintTree(int i, int j) { MTreeNode::MTreeNode(MTreeNode* parent) { m_parent = parent; - m_distance = parent == nullptr ? 0 : parent->m_distance + 1; + if (parent) m_distance = parent->m_distance + 1; } MTreeNode::~MTreeNode() { @@ -29,21 +29,20 @@ bool MTreeNode::addChild(int i, int j) { if (child->m_i == i && child->m_j == j) return false; m_children.push_back(new MTreeNode(this)); - m_children[m_children.size() - 1]->m_distance = this->m_distance + 1; - m_children[m_children.size() - 1]->m_i = i; - m_children[m_children.size() - 1]->m_j = j; + m_children.back()->m_i = i; + m_children.back()->m_j = j; return true; } MTreeNode* MTreeNode::hasChild(int i, int j) const{ - const int count = m_children.size(); - for (auto child : m_children){ + for (auto &child : m_children){ if (child->m_i == i && child->m_j == j) return child; } return nullptr; } +//, const MTreeNode* MTreeNode::searchNode(const int i, const int j) { queue nodes; auto node = searchRoot(this); @@ -60,13 +59,15 @@ const MTreeNode* MTreeNode::searchNode(const int i, const int j) { node = nodes.front(); nodes.pop(); } - return nullptr; } +// const MTreeNode* MTreeNode::searchRoot(const MTreeNode* node) { queue nodes; MTreeNode* parent = node->m_parent; - if (parent) { + if (!parent) + return node; + else { while (true) { MTreeNode* temp = parent->m_parent; if (!temp) break; @@ -74,5 +75,4 @@ const MTreeNode* MTreeNode::searchRoot(const MTreeNode* node) { } return parent; } - else return node; } \ No newline at end of file diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.cpp b/Practise_18.11.20/Practise_18.11.20/Maze.cpp index ebce943..69e32d0 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Maze.cpp @@ -37,15 +37,21 @@ Maze::Maze(int n, int m) { Maze::~Maze() { delete[] m_field; } const MCell& Maze::cell(int i, int j) const { - assert(i >= 0); - assert(j >= 0); + assert(i >= 0 && i < m_height && j >= 0 && j < m_width); return m_field[i * m_width + j]; } -bool Maze::hasConnection(int i1, int j1, int i2, int j2) const { - if ((abs(i1 - i2) + abs(j1 - j2) != 1)) return false; +bool Maze::checkConnection(int i1, int j1, int i2, int j2) const { + assert(i1 == i2 || j1 == j2); + if ((abs(i1 - i2) + abs(j1 - j2) != 1)) + return false; if (j2 < 0 || j2 >= m_width || i2 < 0 || i2 >= m_height) return false; +} + +bool Maze::hasConnection(int i1, int j1, int i2, int j2) const { + if (!checkConnection(i1, j1, i2, j2)) + return false; const int ind1 = min(i1, i2); const int ind2 = min(j1, j2); if (i1 == i2) { @@ -54,16 +60,13 @@ bool Maze::hasConnection(int i1, int j1, int i2, int j2) const { else if (j2 == j1) { return m_field[ind1 * m_width + ind2].down(); } - else - return false; } bool Maze::makeConnection(int i1, int j1, int i2, int j2) { - if ((abs(i1 - i2) + abs(j1 - j2) != 1)) return false; - if (j2 < 0 || j2 >= m_width || i2 < 0 || i2 >= m_height) + if (!checkConnection(i1, j1, i2, j2)) return false; - int ind1 = min(i1, i2); - int ind2 = min(j1, j2); + const int ind1 = min(i1, i2); + const int ind2 = min(j1, j2); if (i1 == i2) { m_field[ind1 * m_width + ind2].m_right = true; return true; @@ -72,13 +75,10 @@ bool Maze::makeConnection(int i1, int j1, int i2, int j2) { m_field[ind1 * m_width + ind2].m_down = true; return true; } - else - return false; } bool Maze::removeConnection(int i1, int j1, int i2, int j2) { - if ((abs(i1 - i2) + abs(j1 - j2) != 1)) return false; - if (j2 < 0 || j2 >= m_width || i2 < 0 || i2 >= m_height) + if (!checkConnection(i1, j1, i2, j2)) return false; int ind1 = min(i1, i2); int ind2 = min(j1, j2); @@ -92,8 +92,6 @@ bool Maze::removeConnection(int i1, int j1, int i2, int j2) { m_field[ind1 * m_width + ind2].m_down = false; return res; } - else - return false; } void Maze::printMaze() const { @@ -112,4 +110,4 @@ void Maze::printMaze() const { if (j + 1 == m_width) cout << endl; } -} \ No newline at end of file +} diff --git a/Practise_18.11.20/Practise_18.11.20/Maze.h b/Practise_18.11.20/Practise_18.11.20/Maze.h index 1fcc379..d5d698a 100644 --- a/Practise_18.11.20/Practise_18.11.20/Maze.h +++ b/Practise_18.11.20/Practise_18.11.20/Maze.h @@ -5,7 +5,8 @@ class Maze { public: Maze(int n, int m); ~Maze(); - const MCell& cell(int i, int j) const; + const MCell& cell(int i, int j) const; + bool checkConnection(int i1, int j1, int i2, int j2) const; bool hasConnection(int i1, int j1, int i2, int j2) const; bool makeConnection(int i1, int j1, int i2, int j2); bool removeConnection(int i1, int j1, int i2, int j2); From df3698fe525866217001ab1ad6690b0f832d2d65 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sun, 27 Dec 2020 01:39:45 +0500 Subject: [PATCH 14/15] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D1=83=D1=81=D1=82=D1=83=D1=8E=20=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D1=83=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=86?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=B0=D0=B6=D0=B4=D0=BE=D0=B3=D0=BE=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp | 2 +- Practise_18.11.20/Practise_18.11.20/MTreeNode.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp index 863fef8..9a5bc43 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp @@ -75,4 +75,4 @@ const MTreeNode* MTreeNode::searchRoot(const MTreeNode* node) { } return parent; } -} \ No newline at end of file +} diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h index a337b78..08b870b 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h @@ -23,4 +23,3 @@ class MTreeNode std::vector m_children; MTreeNode(MTreeNode* parent); }; - From ec258687015f9bb8f050f9303d60df786b79b9b2 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sun, 27 Dec 2020 18:46:02 +0500 Subject: [PATCH 15/15] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20const=20=D0=BA=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?=D0=BC=20searchNode,=20searchRoot;=20=D0=9A=D0=BE=D0=BD=D0=B5?= =?UTF-8?q?=D1=87=D0=BD=D0=BE=20=D0=B6=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=BB=20beginTree)));=20=D1=83=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D0=BB=20=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D1=83=20=D0=B2=20?= =?UTF-8?q?hasChild;=20=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BA=D0=BE?= =?UTF-8?q?=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=BD=D0=BE=20=D1=81=D0=BE=D0=B7?= =?UTF-8?q?=D0=B4=D0=B0=D1=8E=20=D1=8D=D0=BA=D0=B7=D0=B5=D0=BC=D0=BF=D0=BB?= =?UTF-8?q?=D1=8F=D1=80=D1=8B=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Maz?= =?UTF-8?q?e.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp | 8 ++++---- Practise_18.11.20/Practise_18.11.20/MTreeNode.h | 6 +++--- .../Practise_18.11.20/Practise_18.11.20.cpp | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp index 9a5bc43..5808f75 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.cpp @@ -4,7 +4,7 @@ #include "MTreeNode.h" using namespace std; -MTreeNode* MTreeNode::begintTree(int i, int j) { +MTreeNode* MTreeNode::beginTree(int i, int j) { MTreeNode* node = new MTreeNode(nullptr); node->m_i = i; node->m_j = j; @@ -35,7 +35,7 @@ bool MTreeNode::addChild(int i, int j) { } MTreeNode* MTreeNode::hasChild(int i, int j) const{ - for (auto &child : m_children){ + for (auto child : m_children){ if (child->m_i == i && child->m_j == j) return child; } @@ -43,7 +43,7 @@ MTreeNode* MTreeNode::hasChild(int i, int j) const{ } //, -const MTreeNode* MTreeNode::searchNode(const int i, const int j) { +const MTreeNode* MTreeNode::searchNode(const int i, const int j) const{ queue nodes; auto node = searchRoot(this); while (true) @@ -62,7 +62,7 @@ const MTreeNode* MTreeNode::searchNode(const int i, const int j) { } // -const MTreeNode* MTreeNode::searchRoot(const MTreeNode* node) { +const MTreeNode* MTreeNode::searchRoot(const MTreeNode* node) const{ queue nodes; MTreeNode* parent = node->m_parent; if (!parent) diff --git a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h index 08b870b..c1b876b 100644 --- a/Practise_18.11.20/Practise_18.11.20/MTreeNode.h +++ b/Practise_18.11.20/Practise_18.11.20/MTreeNode.h @@ -13,9 +13,9 @@ class MTreeNode const MTreeNode* parent() const { return m_parent; } MTreeNode* hasChild(int i, int j) const; const MTreeNode* child(int i) const { return m_children[i]; } - static MTreeNode* begintTree(int i, int j); - const MTreeNode* searchNode(const int i, const int j); - const MTreeNode* searchRoot(const MTreeNode* node); + static MTreeNode* beginTree(int i, int j); + const MTreeNode* searchNode(const int i, const int j) const; + const MTreeNode* searchRoot(const MTreeNode* node) const; private: int m_i, m_j = 0; int m_distance = 0; diff --git a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp index 96dac3f..3995953 100644 --- a/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp +++ b/Practise_18.11.20/Practise_18.11.20/Practise_18.11.20.cpp @@ -177,17 +177,17 @@ void BuildTreeForMaze2(Maze& maze, const int maze_size, MTreeNode* node) { int main() { const int maze_size = 5; - Maze maze1 = Maze(maze_size, maze_size); + Maze maze1(maze_size, maze_size); BuildMaze1(maze1, maze_size); //Построение простого лабиринта-лесенки cout << "Maze #1:" << endl << endl; maze1.printMaze(); //Печать лабиринта-лесенки - Maze maze2 = Maze(maze_size, maze_size); + Maze maze2(maze_size, maze_size); BuildMaze2(maze2, maze_size); //Построение лабиринта паутинки-лесенки cout << endl << "Maze #2:" << endl << endl; maze2.printMaze(); //Печать лабиринта паутинки-лесенки - MTreeNode* start_node = MTreeNode::begintTree(0, 0); + MTreeNode* start_node = MTreeNode::beginTree(0, 0); cout << endl << "Node tree for Maze #2:" << endl << endl; BuildTreeForMaze2(maze2, maze_size, start_node); //Построение и печать дерева обхода для лабиринта паутинки-лесенки delete start_node; @@ -197,9 +197,9 @@ int main() cin >> height >> width; //Считывание размеров случайного лабиринта auto start_coordinates = GetRandCoordinates(width, height);//Функция для получения произвольных координат на границе лабиринта - MTreeNode* tree = MTreeNode::begintTree(start_coordinates[0], start_coordinates[1]); + MTreeNode* tree = MTreeNode::beginTree(start_coordinates[0], start_coordinates[1]); BuildTree(tree, width, height); //Построение дерева для случайного лабиринта заданного размера - Maze maze3 = Maze(height, width); + Maze maze3(height, width); buildFullMaze(maze3, tree); //Создание лабиринта по дереву cout << endl << "Maze #3:" << endl << endl; maze3.printMaze(); //Печать случайного лабиринта заданного размера