TCS · Question 5
Solve a Maze Problem Using Backtracking
Problem
Find all paths from start to destination in a maze.
Approach
DFS with visited marking counts every simple path to the exit.
Time O(4^(n*m))Space O(n²)
How it works
- 1DFS explores every open neighbour.
- 2Visited marks prevent reuse; unmark on backtrack.
- 3Reaching the exit counts one path.
Sample input / output
Input: 3x3 open maze, start = (0, 0), exit = (2, 2) Output: 12
Constraints
Binary maze; 1 = open, 0 = wall.
Solution — read it, then walk it
Time O(4^(n*m))Space O(n²)Branches 2Returns 2source · C++
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/18
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits