# LeetCode: 695. 岛屿的最大面积¶

## 1、题目描述¶

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]


[[0,0,0,0,0,0,0,0]]


## 2、解题思路¶

​ 对每一个为1的点，发起深度优先搜索，向右和向下搜索，其实与二叉树是一样的，但是不同的是，直接搜索可能放问同样的节点，因此，使用一个访问标记即可，如果该节点访问过了，解放上标记1，没访问过，就是标记0

void dfs(int **grid, int **sign, int *max, int gridRowSize, int gridColSize, int i, int j) {
if (i < 0 || i >= gridRowSize || j < 0 || j >= gridColSize) {
return;
}

if (sign[i][j] == 0) {
if (grid[i][j] == 1) {
*max += 1;
sign[i][j] = 1;
dfs(grid, sign, max, gridRowSize, gridColSize, i - 1, j);
dfs(grid, sign, max, gridRowSize, gridColSize, i, j - 1);
dfs(grid, sign, max, gridRowSize, gridColSize, i + 1, j);
dfs(grid, sign, max, gridRowSize, gridColSize, i, j + 1);
}
} else {
return;
}

}

int maxAreaOfIsland(int **grid, int gridRowSize, int gridColSize) {
int result = 0;
int max = 0;
int **sign = (int **) malloc(sizeof(int *) * gridRowSize);
for (int i = 0;i<gridRowSize;i++){
sign[i] = (int *)malloc(sizeof(int)*gridColSize);
}
for (int i = 0; i < gridRowSize; i++) {
for (int j = 0; j < gridColSize; j++) {
sign[i][j] = 0;
}
}

for (int i = 0; i < gridRowSize; i++) {
for (int j = 0; j < gridColSize; j++) {
if (grid[i][j] == 1) {
max = 0;
dfs(grid, sign, &max, gridRowSize, gridColSize, i, j);
if (result < max) {
result = max;
}
}
}
}

return result;

}