Dsa
Graphs Dfs
Medium
Treasure Island Explorer
You are on a treasure hunt in a forest represented by a grid. Each cell can either be land (0) or water (1). You need to explore the grid using Depth First Search (DFS) to find the maximum number of land cells that can be visited starting from your initial position (denoted by a 2). Each visit to a new land cell is counted as long as it's connected to previously visited cells either horizontally or vertically. Return the maximum number of land cells that can be visited. Input:
- The first line contains two integers R and C (1 <= R, C <= 100), representing the number of rows and columns of the grid.
- The next R lines contain C integers (0, 1, or 2), representing the grid itself.
Output:
- A single integer, the maximum number of land cells that can be visited.
Example Input:
3 4
0 0 1 2
0 0 1 0
1 0 0 0
Example Output:
6
Key concepts
graphsdfspathfinding
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now