-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathinheritance.cpp
57 lines (51 loc) · 1.31 KB
/
inheritance.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
#include<bits/stdc++.h>
using namespace std;
void visited(int i,int j,vector<vector<int>>& grid,int r,int c,int &count)
{
if(i>=r ||i<0 ||j>=c ||j<0 ||grid[i][j]==0 ) return ;
grid[i][j]=0;
count++;
visited(i+1,j+1,grid,r,c,count);
visited(i-1,j+1,grid,r,c,count);
visited(i+1,j-1,grid,r,c,count);
visited(i-1,j-1,grid,r,c,count);
visited(i,j-1,grid,r,c,count);
visited(i,j+1,grid,r,c,count);
visited(i-1,j,grid,r,c,count);
visited(i+1,j,grid,r,c,count);
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int r=grid.size();
int c=grid[0].size();
int count=0;
int max=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(grid[i][j]==1)
{
visited(i,j,grid,r,c,count);
if(count>max)
max=count;
count=0;
}
}
}
return max;
}
int main()
{
int m,n;cin>>m>>n;
vector<vector<int>> grid(m, vector<int> (n));
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
int x;
cin>>x;
grid[i][j]=x;
}
}
cout<<maxAreaOfIsland(grid);
}