-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsaproject.cpp
More file actions
393 lines (333 loc) · 12.7 KB
/
Copy pathdsaproject.cpp
File metadata and controls
393 lines (333 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <queue>
using namespace std;
// Maze dimensions (odd numbers work best for proper wall structure)
const int ROWS = 21; // Should be odd
const int COLS = 21; // Should be odd
// Direction arrays for movement (up, right, down, left)
// For solving the maze, we only move to adjacent cells
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
// Function to initialize maze with all walls (1)
void initializeMaze(int maze[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
maze[i][j] = 1; // Set all cells to walls
}
}
}
// Function to print the maze
void printMaze(int maze[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cout << maze[i][j] << " ";
}
cout << endl;
}
}
// Check if cell is within bounds and is a wall
bool isValid(int maze[ROWS][COLS], int row, int col) {
return (row >= 0 && row < ROWS && col >= 0 && col < COLS && maze[row][col] == 1);
}
// Generate maze using Prim's algorithm
void generateMaze(int maze[ROWS][COLS]) {
// Initialize maze with walls
initializeMaze(maze);
// Starting point (must be odd to maintain wall structure)
int startRow = 1;
int startCol = 1;
maze[startRow][startCol] = 0;
// Arrays to store frontier cells
int frontierRows[ROWS * COLS];
int frontierCols[ROWS * COLS];
int frontierCount = 0;
// Directions for adjacent cells (up, right, down, left)
int primDx[] = {-2, 0, 2, 0};
int primDy[] = {0, 2, 0, -2};
// Add initial frontier cells (2 steps away from start)
for (int i = 0; i < 4; i++) {
int newRow = startRow + primDx[i];
int newCol = startCol + primDy[i];
if (isValid(maze, newRow, newCol)) {
frontierRows[frontierCount] = newRow;
frontierCols[frontierCount] = newCol;
frontierCount++;
}
}
// Main Prim's algorithm loop
while (frontierCount > 0) {
// Pick a random frontier cell
int randIdx = rand() % frontierCount;
int row = frontierRows[randIdx];
int col = frontierCols[randIdx];
// Remove the selected cell from frontier
frontierRows[randIdx] = frontierRows[frontierCount - 1];
frontierCols[randIdx] = frontierCols[frontierCount - 1];
frontierCount--;
// Check adjacent cells (2 steps away) that are already in maze
for (int i = 0; i < 4; i++) {
int adjRow = row + primDx[i];
int adjCol = col + primDy[i];
if (adjRow >= 0 && adjRow < ROWS && adjCol >= 0 && adjCol < COLS &&
maze[adjRow][adjCol] == 0) {
// Connect the frontier cell to the maze
maze[row][col] = 0;
maze[(row + adjRow) / 2][(col + adjCol) / 2] = 0; // Open the wall between
// Add new frontier cells
for (int j = 0; j < 4; j++) {
int newRow = row + primDx[j];
int newCol = col + primDy[j];
if (isValid(maze, newRow, newCol)) {
frontierRows[frontierCount] = newRow;
frontierCols[frontierCount] = newCol;
frontierCount++;
}
}
break; // Only connect to one existing cell
}
}
}
// Ensure entrance and exit
maze[0][1] = 0; // Entrance at top
maze[ROWS-1][COLS-2] = 0; // Exit at bottom
}
// Function to print the solved maze with path
void printSolveMaze(int maze[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (maze[i][j] == 1)
cout << "| ";
else if (maze[i][j] == 2)
cout << "* ";
else
cout << "! "; // Changed to "0 " (no space before 0)
}
cout << endl;
}
}
// Function to solve maze using DFS algorithm
bool solveMazeDFS(int maze[ROWS][COLS], int startRow, int startCol, int endRow, int endCol) {
stack<pair<int, int>> s;
bool visited[ROWS][COLS] = {false}; // Initialize all to false
int parentRow[ROWS][COLS] = {0}; // Initialize all to 0
int parentCol[ROWS][COLS] = {0}; // Initialize all to 0
s.push({startRow, startCol});
visited[startRow][startCol] = true;
bool found = false;
while (!s.empty() && !found) {
// Get the current cell but don't pop yet
pair<int, int> current = s.top();
s.pop(); // Now pop the stack
int row = current.first;
int col = current.second;
// Check if reached the exit
if (row == endRow && col == endCol) {
found = true;
break;
}
// Try all four directions
for (int i = 0; i < 4; i++) {
int newRow = row + dx[i];
int newCol = col + dy[i];
// Check if valid and not visited and is a path (0)
if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS &&
maze[newRow][newCol] == 0 && !visited[newRow][newCol]) {
// Mark as visited and record the parent
s.push({newRow, newCol});
visited[newRow][newCol] = true;
parentRow[newRow][newCol] = row;
parentCol[newRow][newCol] = col;
}
}
}
if (found) {
// Trace back path and count steps
int r = endRow, c = endCol;
int steps = 0;
while (!(r == startRow && c == startCol)) {
maze[r][c] = 2; // Mark path
int tempR = parentRow[r][c];
int tempC = parentCol[r][c];
r = tempR;
c = tempC;
steps++;
}
maze[startRow][startCol] = 2; // Mark start
steps++; // include start point
cout << "\nPath found using DFS!" << endl;
cout << "Steps taken: " << steps << endl;
return true;
} else {
cout << "No path found using DFS.\n";
return false;
}
}
// Function to solve maze using BFS algorithm
bool solveMazeBFS(int maze[ROWS][COLS], int startRow, int startCol, int endRow, int endCol) {
queue<pair<int, int>> q;
bool visited[ROWS][COLS] = {false}; // Initialize all to false
int parentRow[ROWS][COLS] = {0}; // Initialize all to 0
int parentCol[ROWS][COLS] = {0}; // Initialize all to 0
q.push({startRow, startCol});
visited[startRow][startCol] = true;
bool found = false;
while (!q.empty() && !found) {
pair<int, int> current = q.front();
q.pop();
int row = current.first;
int col = current.second;
// Check if reached the exit
if (row == endRow && col == endCol) {
found = true;
break;
}
// Try all four directions
for (int i = 0; i < 4; i++) {
int newRow = row + dx[i];
int newCol = col + dy[i];
// Check if valid and not visited and is a path (0)
if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS &&
maze[newRow][newCol] == 0 && !visited[newRow][newCol]) {
// Mark as visited and record the parent
q.push({newRow, newCol});
visited[newRow][newCol] = true;
parentRow[newRow][newCol] = row;
parentCol[newRow][newCol] = col;
}
}
}
if (found) {
// Trace back path and count steps
int r = endRow, c = endCol;
int steps = 0;
while (!(r == startRow && c == startCol)) {
maze[r][c] = 2; // Mark path
int tempR = parentRow[r][c];
int tempC = parentCol[r][c];
r = tempR;
c = tempC;
steps++;
}
maze[startRow][startCol] = 2; // Mark start
steps++; // include start point
cout << "\nPath found using BFS!" << endl;
cout << "Steps taken: " << steps << endl;
return true;
} else {
cout << "No path found using BFS.\n";
return false;
}
}
// Function to create a copy of the maze
void copyMaze(int source[ROWS][COLS], int destination[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
destination[i][j] = source[i][j];
}
}
}
int main() {
int maze[ROWS][COLS];
int tempMaze[ROWS][COLS]; // For storing a working copy
bool mazeGenerated = false;
// Seed random number generator with current time - do this only once
srand(time(0));
int choice;
char ch;
// Main program loop
do {
cout << "\n=== Maze Generator and Solver ===\n";
cout << "1. Generate maze\n";
cout << "2. Solve maze using DFS\n";
cout << "3. Solve maze using BFS\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1:
cout << "Generating new maze...\n";
generateMaze(maze);
cout << "The generated maze is:\n";
printMaze(maze);
mazeGenerated = true;
break;
case 2:
if (!mazeGenerated) {
cout << "No maze has been generated yet. Generate a maze first.\n";
break;
}
int dfsChoice;
cout << "1. Solve the previously generated maze\n";
cout << "2. Generate a new maze and solve it\n";
cout << "Enter your choice: ";
cin >> dfsChoice;
switch(dfsChoice) {
case 1:
// Create a working copy
copyMaze(maze, tempMaze);
if (solveMazeDFS(tempMaze, 0, 1, ROWS-1, COLS-2)) {
printSolveMaze(tempMaze);
}
break;
case 2:
cout << "Generating new maze...\n";
generateMaze(maze);
cout << "The generated maze is:\n";
printMaze(maze);
// Create a working copy
copyMaze(maze, tempMaze);
if (solveMazeDFS(tempMaze, 0, 1, ROWS-1, COLS-2)) {
printSolveMaze(tempMaze);
}
break;
default:
cout << "Invalid choice\n";
}
break;
case 3:
if (!mazeGenerated) {
cout << "No maze has been generated yet. Generate a maze first.\n";
break;
}
int bfsChoice;
cout << "1. Solve the previously generated maze\n";
cout << "2. Generate a new maze and solve it\n";
cout << "Enter your choice: ";
cin >> bfsChoice;
switch(bfsChoice) {
case 1:
// Create a working copy
copyMaze(maze, tempMaze);
if (solveMazeBFS(tempMaze, 0, 1, ROWS-1, COLS-2)) {
printSolveMaze(tempMaze);
}
break;
case 2:
cout << "Generating new maze...\n";
generateMaze(maze);
cout << "The generated maze is:\n";
printMaze(maze);
// Create a working copy
copyMaze(maze, tempMaze);
if (solveMazeBFS(tempMaze, 0, 1, ROWS-1, COLS-2)) {
printSolveMaze(tempMaze);
}
break;
default:
cout << "Invalid choice\n";
}
break;
case 4:
cout << "Exiting program...\n";
exit(0);
default:
cout << "Invalid choice\n";
}
cout << "Do you want to continue? (y/n): ";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
return 0;
}