This is the code for calculating the number of islands in the grid. Given an m x n
2D binary grid which represents a map of '1's
(land) and '0's
(water), return 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.
(This is a question from leetcode)
Eg-
Input: grid =
[
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3
Constraints:
- `m == grid.length`
- `n == grid[i].length`
- `1 <= m, n <= 300`
- `grid[i][j] is '0' or '1'.`
I have solved it using graphs by finding the total number of connected components. I used a map to point the (i,j)
pair to its corresponding vertex number, an adjacency list to store all the edges, and a visited array to mark the vertices that have already been visited.
Here is the code:
void dfs(vector<vector<int>> edges, bool *visited, int start)
{
visited[start] = true;
int n = edges[start].size();
for (int i = 0; i < n; i++)
{
if (!visited[edges[start][i]])
dfs(edges, visited, edges[start][i]);
}
}
int numIslands(vector<vector<char>> &grid)
{
int m = grid.size();
int n = grid[0].size();
int start = 0;
unordered_map<string, int> map;
//storing the ij pair as a string and giving it an integer value to make it a vertex
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (grid[i][j] == '1')
{
char first = '0' + i;
char second = '0' + j;
string str = "";
str += first;
str += second;
map[str] = start;
start++;
}
}
}
vector<vector<int>> edges(start);
//adjacency list
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (grid[i][j] == '1')
{
char first = '0' + i;
char second = '0' + j;
string str = "";
str += first;
str += second;
if (i - 1 >= 0 && grid[i - 1][j] == '1')
{
char first = '0' + (i - 1);
char second = '0' + j;
string str1 = "";
str1 += first;
str1 += second;
edges[map[str]].push_back(map[str1]);
}
if (i + 1 < m && grid[i + 1][j] == '1')
{
char first = '0' + (i + 1);
char second = '0' + j;
string str1 = "";
str1 += first;
str1 += second;
edges[map[str]].push_back(map[str1]);
}
if (j - 1 >= 0 && grid[i][j - 1] == '1')
{
char first = '0' + i;
char second = '0' + (j - 1);
string str1 = "";
str1 += first;
str1 += second;
edges[map[str]].push_back(map[str1]);
}
if (j + 1 < n && grid[i][j + 1] == '1')
{
char first = '0' + i;
char second = '0' + (j + 1);
string str1 = "";
str1 += first;
str1 += second;
edges[map[str]].push_back(map[str1]);
}
}
}
}
//counting the number of connected components
bool *visited = new bool[start];
for (int i = 0; i < start; i++)
visited[i] = false;
int ans = 0;
for (int i = 0; i < edges.size(); i++)
{
if (!visited[i])
{
ans++;
dfs(edges, visited, i);
}
}
return ans;
}
The code works fine for most of the test cases. However, when I entered one row of input with any number of columns greater than 256, it does not give the desired output(ie, 1).
Eg - [['1', '1', '1', '1', '1', ....(287 times) ]]
gives 32 as output.
Why is that the case?