Easy Approach C++ | Depth First Search | Maximum Number of Fishes in a Grid
Approach\nDepth first Search.\n- Converted each traversed path to land cell so that a separate visited array is not required.\n\n# Code\ncpp []\nclass Solution 
# Approach\nDepth first Search.\n- Converted each traversed path to land cell so that a separate `visited` array is not required.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n    bool isValid(int i, int j, int n, int m){\n        return i>=0 && i<n && j>=0 && j<m;\n    }\n    int dfs(vector<vector<int>>& grid, int row, int col){\n        int n = grid.size(), m = grid[0].size();\n        if(!isValid(row, col, n, m) || grid[row][col] == 0) return 0;\n        int direc[5] = {0, 1, 0, -1, 0};\n        int res = grid[row][col];\n        grid[row][col] = 0;\n        for(int k=0; k<4; k++) { \n            res += dfs(grid, row+direc[k], col+direc[k+1]);\n        }\n        return res;\n    }\n    int findMaxFish(vector<vector<int>>& grid) {\n        int ans = 0;\n        for(int i = 0; i< grid.size(); ++i){\n            for(int j =0; j < grid[0].size(); ++j){\n                ans = max(ans, dfs(grid, i, j));\n            }\n        }\n        return ans;\n    }\n};\n```\n
['Depth-First Search', 'Graph', 'C++']