Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
225 lines (188 loc) · 6.13 KB
/
Copy pathmain.cpp
File metadata and controls
225 lines (188 loc) · 6.13 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
//
// Created by Manju Muralidharan on 11/22/25.
//
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
// Directions for DFS (students must use these)
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
// ----------------------------------------------------------
// DO NOT MODIFY: Maze generation
// ----------------------------------------------------------
void generateMaze(vector<vector<int>>& maze, int N, int M) {
srand(time(0));
for (int r = 0; r < N; r++) {
for (int c = 0; c < M; c++) {
int roll = rand() % 100;
maze[r][c] = (roll < 70) ? 0 : 1; // 0 = open, 1 = wall
}
}
}
// ----------------------------------------------------------
// DO NOT MODIFY: Choose a random boundary cell that is open
// ----------------------------------------------------------
pair<int,int> chooseBoundaryCell(const vector<vector<int>>& maze) {
int N = maze.size();
int M = maze[0].size();
while (true) {
int side = rand() % 4;
int r, c;
if (side == 0) { // top row
r = 0;
c = rand() % M;
} else if (side == 1) { // bottom row
r = N - 1;
c = rand() % M;
} else if (side == 2) { // left column
r = rand() % N;
c = 0;
} else { // right column
r = rand() % N;
c = M - 1;
}
if (maze[r][c] == 0) {
return {r, c};
}
}
}
// ----------------------------------------------------------
// DO NOT MODIFY: Print the maze with S and E markers
// ----------------------------------------------------------
void printMaze(const vector<vector<int>>& maze,
int ent_r, int ent_c,
int exit_r, int exit_c)
{
int N = maze.size();
int M = maze[0].size();
cout << "\nMaze:\n";
for (int r = 0; r < N; r++) {
for (int c = 0; c < M; c++) {
if (r == ent_r && c == ent_c) {
cout << "S ";
} else if (r == exit_r && c == exit_c) {
cout << "E ";
} else {
cout << maze[r][c] << " ";
}
}
cout << "\n";
}
}
// ----------------------------------------------------------
// DO NOT MODIFY: Print the reconstructed path
// Students must fill parent[][] correctly during DFS
// ----------------------------------------------------------
void printPath(pair<int,int> exitcell,
const vector<vector<int>>& parent_r,
const vector<vector<int>>& parent_c,
int ent_r, int ent_c)
{
int r = exitcell.first;
int c = exitcell.second;
vector<pair<int,int>> path;
// Walk backward from exit to entrance
while (!(r == ent_r && c == ent_c)) {
path.push_back({r, c});
int pr = parent_r[r][c];
int pc = parent_c[r][c];
r = pr;
c = pc;
}
path.push_back({ent_r, ent_c});
cout << "\nPath from entrance to exit:\n";
for (int i = path.size() - 1; i >= 0; i--) {
cout << "(" << path[i].first << ", " << path[i].second << ")\n";
}
}
// ----------------------------------------------------------
// STUDENTS IMPLEMENT DFS HERE
// Add arguments, return type, and logic
// ----------------------------------------------------------
bool dfs(int r, int c,
const vector<vector<int>>& maze,
vector<vector<bool>>& visited,
vector<vector<int>>& parent_r,
vector<vector<int>>& parent_c,
int exit_r, int exit_c) {
int N = maze.size();
int M = maze[0].size();
//Out of Bounds
if (r < 0 || c < 0 || r >= N || c >= M) {
return false;
}
//Base case to check if current position is exit
if (r == exit_r && c == exit_c) {
return true;
}
//Mark as Visited
visited[r][c] = true;
//Look at all the directions (up,down,right,left) Neighbors
for (int i = 0; i < 4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
//Going to look at 0,-1,1 values (Out of bounds)
if (nr < 0 || nr >= N || nc < 0 || nc >= M) {
continue;
}
//Wall Check
if (maze[nr][nc] == 1) {
continue;
}
//Visited?
if (visited[nr][nc]) {
continue;
}
//All passed set parents as nc and nr
parent_r[nr][nc] = r;
parent_c[nr][nc] = c;
if (dfs(nr, nc, maze, visited, parent_r, parent_c, exit_r, exit_c)) {
return true;
}
}
return false;
}
// ----------------------------------------------------------
// MAIN PROGRAM (students add DFS calls and logic)
// ----------------------------------------------------------
int main() {
int N, M;
cout << "Enter maze dimensions N M: ";
cin >> N >> M;
vector<vector<int>> maze(N, vector<int>(M));
generateMaze(maze, N, M);
// Pick entrance and exit
pair<int,int> entrance = chooseBoundaryCell(maze);
pair<int,int> exitcell = chooseBoundaryCell(maze);
while (exitcell == entrance) {
exitcell = chooseBoundaryCell(maze);
}
int ent_r = entrance.first;
int ent_c = entrance.second;
int exit_r = exitcell.first;
int exit_c = exitcell.second;
// Display the maze
printMaze(maze, ent_r, ent_c, exit_r, exit_c);
// Students must use these
vector<vector<bool>> visited(N, vector<bool>(M, false));
vector<vector<int>> parent_r(N, vector<int>(M, -1));
vector<vector<int>> parent_c(N, vector<int>(M, -1));
// ------------------------------------------------------
// STUDENT WORK:
// Call your DFS, track visited, and fill parent_r and parent_c
// ------------------------------------------------------
bool found = dfs(ent_r, ent_c, maze, visited, parent_r, parent_c, exit_r, exit_c);
// ------------------------------------------------------
// STUDENT WORK:
// If found, print the path
// ------------------------------------------------------
if (found) {
printPath(exitcell, parent_r, parent_c, ent_r, ent_c);
} else {
cout << "\nNo path exists.\n";
// }
return 0;
}
}