-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path695.cpp
28 lines (27 loc) · 852 Bytes
/
695.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
// dfs.cpp
class Solution {
int row, col;
int dfs(vector<vector<int>> &grid, vector<vector<int>> &visited, int i,
int j) {
if (i < 0 || i >= row || j < 0 || j >= col || visited[i][j] ||
grid[i][j] == 0)
return 0;
visited[i][j] = 1;
return 1 + dfs(grid, visited, i + 1, j) + dfs(grid, visited, i, j + 1) +
dfs(grid, visited, i - 1, j) + dfs(grid, visited, i, j - 1);
}
public:
int maxAreaOfIsland(vector<vector<int>> &grid) {
row = grid.size();
if (row == 0)
return 0;
col = grid[0].size();
int maxArea = 0;
vector<vector<int>> visited(row, vector<int>(col, 0));
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j)
if (visited[i][j] == 0 && grid[i][j] == 1)
maxArea = max(maxArea, dfs(grid, visited, i, j));
return maxArea;
}
};