其实该题为求连通子图的个数,可以借助DFS的思想:
class Solution {
public:
bool dfs(int i,int j,vector<vector<char>> &grid)
{
if(i>=0&&i<grid.size()&&j>=0&&j<grid[i].size()&&grid[i][j]==‘1‘)
{
grid[i][j] = ‘3‘;
dfs(i-1,j,grid);
dfs(i+1,j,grid);
dfs(i,j-1,grid);
dfs(i,j+1,grid);
return true;
}
return false;
}
int numIslands(vector<vector<char>> &grid) {
int num = 0;
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[i].size();j++)
{
if(dfs(i,j,grid))
num++;
}
}
return num;
}
};
原文地址:http://blog.csdn.net/majing19921103/article/details/45112755