Leetcode 200. 岛屿的个数

求连通量的个数

class Solution {
public:
    int n,m;
    int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    bool _place(int x,int y){
        return x>=0 && x=0 &&yvoid dfs(vector<vector<char>>& grid,int x,int y){
        grid[x][y]='0';
        for(int i=0;i<4;++i)
            if(_place(x+dx[i],y+dy[i]) && grid[x+dx[i]][y+dy[i]]=='1') dfs(grid,x+dx[i],y+dy[i]);
    }
    int numIslands(vector<vector<char>>& grid) {
        n=grid.size(),m=n?grid[0].size():0;
        int ans=0;
        for(int i=0;ifor(int j=0;jif(grid[i][j]=='1') dfs(grid,i,j),++ans;
        return ans;                
    }
};

你可能感兴趣的:(LeetCode)