Depth-First Search (DFS)

T: O(rows × cols)
Feedback

Depth-First Search (DFS)

Start: put the start cell on the stack.
S0,10,20,40,51,01,21,42,02,22,32,42,53,03,13,23,54,04,24,34,44,5
Visited
On stack
Current
Pushing
SpeedNormal (500ms)
Parameters

{ grid: number[][] (0=empty, 1=wall), startRow, startCol }

Variables
stack (top →)[(0,0)]
visited cells0
1DFS(grid, start):
2 stack = [start]
3 while stack not empty:
4 cell = stack.pop() ← take from top (last in, first out)
5 if cell already visited: continue
6 mark cell as visited
7 for each neighbor (up/down/left/right):
8 if neighbor empty: stack.push(neighbor)
9 done
Output
Start: put the start cell on the stack.