Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input: 1 1 1 1 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 Output: 1
Example 2:
Input: 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1 Output: 3
The core challenge of this problem is to identify and count distinct islands in a 2D grid. An island is defined as a group of horizontally or vertically connected '1's (land). The problem is significant in various applications such as geographical mapping, image processing, and network connectivity.
Potential pitfalls include not properly handling the boundaries of the grid and not marking visited cells, which can lead to incorrect counts or infinite loops.
To solve this problem, we can use Depth-First Search (DFS) or Breadth-First Search (BFS) to traverse the grid. The idea is to iterate through each cell in the grid, and when a '1' is found, initiate a DFS/BFS to mark all connected '1's as visited. This ensures that each island is counted only once.
A naive solution would involve checking each cell and its neighbors repeatedly, leading to redundant checks and higher time complexity. This approach is not optimal.
The optimized solution involves using DFS or BFS to mark all cells of an island as visited once an unvisited '1' is found. This reduces redundant checks and ensures each cell is processed only once.
Here is a step-by-step breakdown of the DFS approach:
#include <vector>
using namespace std;
// Function to perform DFS and mark visited cells
void dfs(vector<vector<char>>& grid, int i, int j) {
// Check for boundary conditions and if the cell is water or already visited
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || grid[i][j] == '0') {
return;
}
// Mark the cell as visited by setting it to '0'
grid[i][j] = '0';
// Recursively visit all adjacent cells
dfs(grid, i + 1, j); // Down
dfs(grid, i - 1, j); // Up
dfs(grid, i, j + 1); // Right
dfs(grid, i, j - 1); // Left
}
int numIslands(vector<vector<char>>& grid) {
if (grid.empty()) return 0;
int num_islands = 0;
// Iterate through each cell in the grid
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
// If a '1' is found, it's a new island
if (grid[i][j] == '1') {
++num_islands;
// Perform DFS to mark all connected '1's
dfs(grid, i, j);
}
}
}
return num_islands;
}
The time complexity of this approach is O(M*N), where M is the number of rows and N is the number of columns in the grid. This is because each cell is visited once. The space complexity is O(M*N) in the worst case due to the recursion stack in DFS.
Potential edge cases include:
Each of these cases should be tested to ensure the algorithm handles them correctly.
To test the solution comprehensively, include a variety of test cases:
Using a testing framework like Google Test can help automate and manage these tests effectively.
When approaching such problems, consider the following tips:
Understanding and solving the "Number of Islands" problem is crucial for developing skills in grid traversal and graph algorithms. By practicing and exploring different approaches, one can improve their problem-solving abilities and prepare for more complex challenges.
For further reading and practice, consider the following resources: