|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#include <thread> |
| 3 | +#include <chrono> |
| 4 | +#include <cstdlib> |
| 5 | +#include <ctime> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | +using namespace std::this_thread; |
| 9 | +using namespace std::chrono; |
| 10 | + |
| 11 | +const int SIZE = 10; |
| 12 | +int delay_ms = 120; |
| 13 | + |
| 14 | +bool visited[SIZE * SIZE]; |
| 15 | +bool walls[SIZE * SIZE][2] = {0}; |
| 16 | +int startPos = 0; |
| 17 | +int goalPos = SIZE * SIZE - 1; |
| 18 | +int path[SIZE * SIZE] = {0}; |
| 19 | +int pos = startPos; |
| 20 | +int rot = 0; |
| 21 | +int pathIndex = 0; |
| 22 | + |
| 23 | +// ------------------------------------------------------------ |
| 24 | +void drawMazeConsole(bool showPath = false, bool clearScreen = true) { |
| 25 | + if (clearScreen) { |
| 26 | +#ifdef _WIN32 |
| 27 | + system("cls"); |
| 28 | +#else |
| 29 | + system("clear"); |
| 30 | +#endif |
| 31 | + } |
| 32 | + |
| 33 | + cout << "\n"; |
| 34 | + for (int j = 0; j < SIZE; j++) { |
| 35 | + for (int i = 0; i < SIZE; i++) { |
| 36 | + cout << "+"; |
| 37 | + if (walls[i + j * SIZE][1]) |
| 38 | + cout << " "; |
| 39 | + else |
| 40 | + cout << "---"; |
| 41 | + } |
| 42 | + cout << "+\n"; |
| 43 | + |
| 44 | + for (int i = 0; i < SIZE; i++) { |
| 45 | + if (walls[i + j * SIZE][0]) |
| 46 | + cout << " "; |
| 47 | + else |
| 48 | + cout << "|"; |
| 49 | + |
| 50 | + int position = i + j * SIZE; |
| 51 | + |
| 52 | + if (position == startPos) |
| 53 | + cout << " S "; |
| 54 | + else if (position == goalPos) |
| 55 | + cout << " G "; |
| 56 | + else if (position == pos && showPath) |
| 57 | + cout << " P "; |
| 58 | + else if (showPath && path[position]) |
| 59 | + cout << " . "; |
| 60 | + else |
| 61 | + cout << " "; |
| 62 | + } |
| 63 | + cout << "|\n"; |
| 64 | + } |
| 65 | + |
| 66 | + for (int i = 0; i < SIZE; i++) cout << "+---"; |
| 67 | + cout << "+\n"; |
| 68 | + cout.flush(); |
| 69 | + sleep_for(milliseconds(delay_ms)); |
| 70 | +} |
| 71 | + |
| 72 | +// ------------------------------------------------------------ |
| 73 | +int nextCell(int pos, int size) { |
| 74 | + int options[4], count = 0; |
| 75 | + if (pos >= size && !visited[pos - size]) options[count++] = -size; |
| 76 | + if ((pos + 1) % size != 0 && !visited[pos + 1]) options[count++] = 1; |
| 77 | + if (pos < size * (size - 1) && !visited[pos + size]) options[count++] = size; |
| 78 | + if (pos % size != 0 && !visited[pos - 1]) options[count++] = -1; |
| 79 | + |
| 80 | + if (count == 0) return 0; |
| 81 | + return options[rand() % count]; |
| 82 | +} |
| 83 | + |
| 84 | +void connect(int pos1, int pos2) { |
| 85 | + if (pos2 > pos1) { |
| 86 | + if (pos2 == pos1 + 1) walls[pos2][0] = 1; |
| 87 | + else walls[pos2][1] = 1; |
| 88 | + } else { |
| 89 | + if (pos1 == pos2 + 1) walls[pos1][0] = 1; |
| 90 | + else walls[pos1][1] = 1; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void randomDFS(int pos, int size) { |
| 95 | + visited[pos] = 1; |
| 96 | + int next = pos + nextCell(pos, size); |
| 97 | + |
| 98 | + while (next != pos) { |
| 99 | + connect(pos, next); |
| 100 | + pos = next; |
| 101 | + drawMazeConsole(false); |
| 102 | + randomDFS(pos, size); |
| 103 | + next = pos + nextCell(pos, size); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// ------------------------------------------------------------ |
| 108 | +bool move() { |
| 109 | + char dir; |
| 110 | + if (abs(rot) % 4 == 0) dir = 'N'; |
| 111 | + else if (abs(rot) % 4 == 1) dir = 'E'; |
| 112 | + else if (abs(rot) % 4 == 2) dir = 'S'; |
| 113 | + else dir = 'W'; |
| 114 | + |
| 115 | + switch (dir) { |
| 116 | + case 'E': |
| 117 | + if ((pos + 1) % SIZE != 0 && walls[pos + 1][0]) { pos += 1; path[pos] = ++pathIndex; return true; } |
| 118 | + return false; |
| 119 | + case 'S': |
| 120 | + if (pos < SIZE * (SIZE - 1) && walls[pos + SIZE][1]) { pos += SIZE; path[pos] = ++pathIndex; return true; } |
| 121 | + return false; |
| 122 | + case 'W': |
| 123 | + if (pos % SIZE != 0 && walls[pos][0]) { pos -= 1; path[pos] = ++pathIndex; return true; } |
| 124 | + return false; |
| 125 | + case 'N': |
| 126 | + if (pos >= SIZE && walls[pos][1]) { pos -= SIZE; path[pos] = ++pathIndex; return true; } |
| 127 | + return false; |
| 128 | + } |
| 129 | + return false; |
| 130 | +} |
| 131 | + |
| 132 | +void solveMazeRightHand() { |
| 133 | + path[0] = 1; |
| 134 | + rot = 1; |
| 135 | + pos = startPos; |
| 136 | + |
| 137 | + while (pos != goalPos) { |
| 138 | + rot++; |
| 139 | + if (!move()) { |
| 140 | + rot--; |
| 141 | + if (!move()) { |
| 142 | + rot--; |
| 143 | + if (!move()) { |
| 144 | + rot--; |
| 145 | + move(); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + drawMazeConsole(true); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// ------------------------------------------------------------ |
| 154 | +int main() { |
| 155 | + srand(time(0)); |
| 156 | + |
| 157 | + memset(visited, 0, sizeof(visited)); |
| 158 | + cout << "\nGenerating maze...\n"; |
| 159 | + randomDFS(0, SIZE); |
| 160 | + |
| 161 | + cout << "\nMaze generation complete!\n"; |
| 162 | + drawMazeConsole(false, false); // show final maze once |
| 163 | + sleep_for(seconds(2)); |
| 164 | + |
| 165 | + cout << "\nSolving maze (Right-Hand Rule)...\n"; |
| 166 | + solveMazeRightHand(); |
| 167 | + |
| 168 | + cout << "\nFinal solved maze:\n"; |
| 169 | + drawMazeConsole(true, false); |
| 170 | + |
| 171 | + cout << "\nPress Enter to exit..."; |
| 172 | + cin.ignore(); |
| 173 | + cin.get(); |
| 174 | + return 0; |
| 175 | +} |
0 commit comments