标签:size cto ++ ber code == false oid span
class Solution { public: int numIslands(vector<vector<char>>& grid) { if (grid.empty() || grid[0].empty()) return 0; int count=0; int m = grid.size(), n = grid[0].size(); vector<vector<bool>> visit(m, vector<bool>(n, false)); for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(grid[i][j]==‘1‘&&!visit[i][j]){ dfs(grid,visit,i,j); ++count; } }} return count; } void dfs(vector<vector<char>>& grid, vector<vector<bool>> &visit,int x,int y){ if (x < 0 || x >= grid.size()) return; if (y < 0 || y >= grid[0].size()) return; if (grid[x][y] != ‘1‘ || visit[x][y]) return; visit[x][y] = true; dfs(grid, visit, x - 1, y); dfs(grid, visit, x + 1, y); dfs(grid, visit, x, y - 1); dfs(grid, visit, x, y + 1); } };
标签:size cto ++ ber code == false oid span
原文地址:https://www.cnblogs.com/illfuckingkyzb/p/10193442.html