-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberofIslands_200.cpp
42 lines (40 loc) · 985 Bytes
/
NumberofIslands_200.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int numIslands(vector<vector<char> >& grid)
{
int counter = 0;
if (grid.empty() || grid[0].empty())
return 0;
else
{
for (int i = 0; i < grid.size(); i++)
for (int j = 0; j < grid[0].size(); j++)
if (grid[i][j] == '1')
{
DFSFill(grid, i, j);
counter++;
}
return counter;
}
}
void DFSFill(vector<vector<char> >&grid, int i, int j)
{
if (i >= 0 && j >= 0 && i < grid.size() && j < grid[0].size() && grid[i][j] == '1')
{
grid[i][j] = '0';
DFSFill(grid, i + 1, j);
DFSFill(grid, i - 1, j);
DFSFill(grid, i , j + 1);
DFSFill(grid, i , j - 1);
}
}
};
int main()
{
Solution s;
vector<vector<char> > v;
cout << s.numIslands(v) << endl;
}