点击打开链接
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <vector>
#include <numeric>
#include <limits>
#include <math.h>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
class Solution {
public:
void dfs(int x, int y, vector<vector<char>>& grid)
{
if((x<0)||(y<0)||(x>=grid.size())||(y>=grid[0].size()))
return;
else if(grid[x][y]=='1'){
grid[x][y] = '0';
dfs(x-1, y, grid);
dfs(x+1, y, grid);
dfs(x, y-1, grid);
dfs(x, y+1, grid);
}
}
int numIslands(vector<vector<char>>& grid) {
int result =0;
if(grid.size()==0) return 0;
int row =grid.size(), col = grid[0].size();
int i=0, j=0;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(grid[i][j]=='1'){
result+=1;
// grid[i][j]='0';
dfs(i,j,grid);
}
}
}
return result;
}
};
int main()
{
return 0;
}