0

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;
}  
ForceBru
  • 36,993
  • 10
  • 54
  • 78
  • 1
    See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). In addition, it is notorious that geeksforgeeks is a bad site with a lot of mistakes. – prapin May 23 '21 at 13:19
  • `Dfs` takes `vector>` as the first parameter. `numIslands` attempts to pass `vector>` instead. – Igor Tandetnik May 23 '21 at 14:33
  • [Edit the question](https://stackoverflow.com/posts/67659583/edit) and show the code you are now compiling, and the error you are now seeing. – Igor Tandetnik May 23 '21 at 17:42

0 Answers0