Dsa
Graphs Dfs
Medium
Orchard Connectivity
You are given a grid representing an orchard. The grid is filled with '.' for empty plots and 'O' for occupied plots. The goal is to determine how many separate connected components of occupied plots (trees) are present in the grid. Two plots are considered connected if they are adjacent horizontally or vertically.
Input format:
- The first line contains two integers R and C (1 <= R, C <= 100), indicating the number of rows and columns in the grid.
- The next R lines each contain a string of length C consisting of 'O' or '.'.
Output format:
- A single integer representing the number of separate connected components of trees.
Example:
Input:
3 3
O.O
OO.
...
Output:
1
Key concepts
graphsdfsconnected_components
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