岛屿的最大面积
class Solution {
public:
const static int g_dirMaxLen = 4;
const static int g_dirMaxDim = 2;
int dir[g_dirMaxLen][g_dirMaxDim] = {
{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int dfs(vector<vector<int>>& grid, int i, int j, int row, int col) {
if (i < 0 || i >= row || j < 0 || j >= col || grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
int num = 1;
for (int k = 0; k < g_dirMaxLen; k++) {
num += dfs(grid, i + dir[k][0], j + dir[k][1], row, col);
}
return num;
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int row = grid.size();
int col = grid[0].size();
int res = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == 1) {
res = max(res, dfs(grid, i, j, row, col));
}
}
}
return res;
}
};