-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.c
33 lines (28 loc) · 965 Bytes
/
dfs.c
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// https://leetcode.cn/problems/max-area-of-island/
// 岛屿的最大面积
int dfs(int **grid, int gridSize, int* gridColSize, int i, int j) {
if (i < 0 || i >= gridSize || j < 0 || j >= gridColSize[0] || grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
return 1 + dfs(grid,gridSize, gridColSize, i, j - 1) +
dfs(grid,gridSize, gridColSize, i, j + 1) +
dfs(grid,gridSize, gridColSize, i - 1, j) +
dfs(grid,gridSize, gridColSize, i + 1, j);
}
int maxAreaOfIsland(int** grid, int gridSize, int* gridColSize){
int max = 0;
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridColSize[0]; j++) {
int area = 0;
if (grid[i][j] == 1) {
area = dfs(grid, gridSize, gridColSize, i, j);
}
max = max > area ? max : area;
}
}
return max;
}