I had made this code myself but there is always an error coming. My logic is that i created a 2d vector of bool and run a dfs in it. And everytime it find '1' it calls for dfs. But I think the error is from how I created the bool vector. The error says this
51:45: error: no matching function for call to ‘Solution::Dfs(std::vector >&, std::vector >&, int&, int&, int&, int&)’ Dfs(grid, vec, i, j, n, m);
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to find the number of islands.
void Dfs(vector<vector<int>> grid, vector<vector<bool>> &vec, int i, int j, int x, int y){
vec[i][j]=true;
if(i+1<x && grid[i+1][j]=='1' && !vec[i+1][j]){
Dfs(grid, vec, i+1, j, x, y);
}
if(i-1>=0 && grid[i-1][j]=='1' && !vec[i-1][j]){
Dfs(grid, vec, i-1, j, x, y);
}
if(j-1>=0 && grid[i][j-1]=='1' && !vec[i][j-1]){
Dfs(grid, vec, i, j-1, x, y);
}
if(j+1<y && grid[i][j+1]=='1' && !vec[i][j+1]){
Dfs(grid, vec, i, j+1, x, y);
}
if(i+1<x && j+1<y && grid[i+1][j+1]=='1' && !vec[i+1][j+1]){
Dfs(grid, vec, i+1, j+1, x, y);
}
if(i-1>=0 && j-1>=0 && grid[i-1][j-1]=='1' && !vec[i-1][j-1]){
Dfs(grid, vec, i-1, j-1, x, y);
}
if(i-1>=0 && j+1<y && grid[i-1][j+1]=='1' && !vec[i-1][j+1]){
Dfs(grid, vec, i-1, j+1, x, y);
}
if(i+1>=0 && j-1>=0 && grid[i+1][j-1]=='1' && !vec[i+1][j-1]){
Dfs(grid, vec, i+1, j-1, x, y);
}
}
int numIslands(vector<vector<char>>& grid)
{
vector<vector<bool>> vec(grid.size(), vector<bool>(grid[0].size(), 0));
int count=0, n=grid.size(), m=grid[0].size();
for(int i=0;i<grid.size();i++){
for(int j=0;j<grid[0].size();j++){
if(grid[i][j]=='1' && vec[i][j]==false){
count++;
Dfs(grid, vec, i, j, n, m);
}
}
}
return count;
}
};
// { Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int n, m;
cin >> n >> m;
vector<vector<char>>grid(n, vector<char>(m, '#'));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> grid[i][j];
}
}
Solution obj;
int ans = obj.numIslands(grid);
cout << ans <<'\n';
}
return 0;
}