-
Notifications
You must be signed in to change notification settings - Fork 66
/
09-Largest Island.cpp
43 lines (42 loc) · 1.23 KB
/
09-Largest Island.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
#include<bits/stdc++.h>
using namespace std;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int maxAreaOfIsland(vector<vector<int>> grid) {
int n=grid.size(),m=grid[0].size(),ans=0;
if(n==0)
{
return ans;
}
vector<vector<int>>visit(n,vector<int>(m));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int cnt=0;
if(grid[i][j]==1 && !visit[i][j])
{
cnt++;
queue<pair<int,int>>q;
q.push({i,j});
visit[i][j]=1;
while(!q.empty())
{
pair<int,int>p=q.front();
q.pop();
for(int k=0;k<4;k++)
{
int pos=p.first+dx[k],pos1=p.second+dy[k];
if(pos>=0 && pos1>=0 && pos<n && pos1<m && !visit[pos][pos1] && grid[pos][pos1]==1)
{
visit[pos][pos1]=1;
cnt++;
q.push({pos,pos1});
}
}
}
ans=max(ans,cnt);
}
}
}
return ans;
}