-
Notifications
You must be signed in to change notification settings - Fork 12
/
MaxAreaOfIsland.cpp
43 lines (35 loc) · 1.15 KB
/
MaxAreaOfIsland.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
class Solution {
public:
void check(vector<vector<int> >& grid, vector<vector<bool> >& visited, int& temp, int x, int y){
if(x >= grid.size() || x < 0 || y < 0 || y >= grid[0].size()){
return;
}
else if(grid[x][y] == 0 || visited[x][y]){
return;
}
visited[x][y] = true;
temp++;
check(grid, visited, temp, x-1, y);
check(grid, visited, temp, x+1, y);
check(grid, visited, temp, x, y-1);
check(grid, visited, temp, x, y+1);
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int rows = grid.size(), ans = 0;
if(!rows){
return ans;
}
int cols = grid[0].size();
vector<vector<bool> > visited(rows, vector<bool>(cols, false));
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(!visited[i][j]){
int temp = 0;
check(grid, visited, temp, i, j);
ans = max(ans, temp);
}
}
}
return ans;
}
};