Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
850 B
1
Indexable
Never
class Solution {
public:
    int r,c;
    bool inr(int x,int y){
        return (x>=0 && x<r && y>=0 && y<c);
    }

    
    vector<pair<int,int>>dir={{1,0},{-1,0},{0,1},{0,-1}};

    int dfs(int x,int y,vector<vector<int>>& grid){
        if(!inr(x,y) || grid[x][y]==0) return 0;
        int sum=grid[x][y];
        grid[x][y]=0;
        for(auto d:dir){
            int new_x=x+d.first;
            int new_y=y+d.second;
            //if(inr(new_x,new_y) && grid[new_x][new_y]!=0){
                sum+=dfs(new_x,new_y,grid);
            //}
        }
        return sum;
    }
    int findMaxFish(vector<vector<int>>& grid) {
        r=grid.size(), c=grid[0].size();
        int res=0;
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res=max(res,dfs(i,j,grid));
            }
        }
        return res;
    }
};
Leave a Comment