-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path200.cpp
66 lines (50 loc) · 1.16 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution {
public:
int n;
int m;
int visited[1000][1000];
void dfs(int r,int s,vector<vector<char>>&A)
{
if(r<0 ||r>=n || s<0 || s>=m)
{
return;
}
if(A[r][s]=='0' || visited[r][s]==1)
{
return ;
}
visited[r][s]=1;
dfs(r+1,s,A);
dfs(r-1,s,A);
dfs(r,s+1,A);
dfs(r,s-1,A);
}
int numIslands(vector<vector<char>>& A) {
n=A.size();
if(n==0)
{
return 0;
}
m=A[0].size();
cout<<n<<" "<<m<<" ";
for(int i = 0; i < n; i++){
for(int j=0;j<m;j++)
{
visited[i][j]=0;
}
}
int count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(A[i][j]=='1' && visited[i][j]==0)
{
count++;
dfs(i,j,A);
}
}
}
return count;
}
};