Starter Code README
This project provides you with a randomly generated maze stored in a 2D matrix. Your job is to implement a recursive Depth-First Search (DFS) that determines whether there is a path from the entrance to the exit.
The starter code already includes:
- Maze generation
- Entrance and exit selection
- Maze printing
- Data structures for visited tracking
- Data structures for tracking parent cells
- A function to print the final reconstructed path
You must implement the DFS logic yourself.
The maze is stored in:
vector<vector<int>> maze;
Each cell contains:
0meaning the cell is open1meaning the cell is a wall
Movement is allowed only in four directions: up, right, down, left.
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
These arrays represent the change in row and column for each direction.
| Index | Direction | dr | dc |
|---|---|---|---|
| 0 | Up | -1 | 0 |
| 1 | Right | 0 | +1 |
| 2 | Down | +1 | 0 |
| 3 | Left | 0 | -1 |
Two open boundary cells are randomly chosen:
pair<int,int> entrance;
pair<int,int> exitcell;
They are extracted for convenience into:
int ent_r, ent_c;
int exit_r, exit_c;
vector<vector<bool>> visited(N, vector<bool>(M, false));
This tracks whether DFS has visited a cell.
You must mark each visited cell in your DFS implementation.
To reconstruct the path, the program provides:
vector<vector<int>> parent_r; // row of parent cell
vector<vector<int>> parent_c; // column of parent cell
When DFS moves from (r, c) to (nr, nc) you must set:
parent_r[nr][nc] = r;
parent_c[nr][nc] = c;
This allows the starter code to rebuild the path from exit to entrance.
Creates the random maze.
Selects an open boundary cell for the entrance or exit.
Prints the maze with S marking the entrance and E marking the exit.
Uses the parent arrays to reconstruct and print the full path from the entrance to the exit.
You must implement:
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);
Your DFS must handle the following:
- Out-of-bounds checks
- Wall checks (
maze[r][c] == 1) - Visited checks
- Marking the current cell as visited
- Checking if
(r, c)is the exit - Exploring neighbors using
dranddc - Assigning the parent before recursing
- Returning true when the exit is found
In main(), you will add:
bool found = dfs(ent_r, ent_c, maze, visited, parent_r, parent_c, exit_r, exit_c);If DFS succeeds:
printPath(exitcell, parent_r, parent_c, ent_r, ent_c);Otherwise:
cout << "\nNo path exists.\n";| Variable | Meaning |
|---|---|
maze |
The maze grid of 0s and 1s |
visited |
Tracks which cells DFS has visited |
parent_r, parent_c |
Store parent coordinates for path reconstruction |
dr, dc |
Direction arrays for movement |
entrance, exitcell |
Boundary start and end cells |
ent_r, ent_c |
Entrance coordinates |
exit_r, exit_c |
Exit coordinates |
N, M |
Maze dimensions |
Your submission must include:
- Your completed DFS implementation
- Correct use of visited and parent arrays
- A successful path printout when a path exists
Do not modify the maze generator or the provided print functions.