leetcode695 C++ 28ms 最大岛屿面积



class Solution {
public:
    int rec(vector<vector<int>>& grid, int i, int j){
        if(i>=grid.size() || j>= grid[0].size()){
            return 0;
        }
        if(grid[i][j] == 1){
            grid[i][j] = 0;
            return 1 + rec(grid, i+1, j) + rec(grid, i, j+1) + rec(grid, i-1, j) + rec(grid, i, j-1);
        }
        else{
            return 0;
        }
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int res=0;
        for(int i=0;i<grid.size();i++){
            for(int j=0;j<grid[0].size();j++){
                if(grid[i][j] == 1){
                    grid[i][j] = 0;
                    res = max(res, 1 + rec(grid, i+1, j) + rec(grid, i, j+1));
                }
            }
        }
        return res;
    }

};
posted @ 2018-08-04 22:34  一条图图犬  阅读(708)  评论(0编辑  收藏  举报