Ref: https://pdai.tech/md/algorithm/alg-core-search.html
广度优先搜索(Depth-First Search,DFS)
2.经典案例
2.1 岛屿连通面积
Ref: https://leetcode-cn.com/problems/max-area-of-island/
private int m, n;private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};public int maxAreaOfIsland(int[][] grid) {if (grid == null || grid.length == 0) {return 0;}m = grid.length;n = grid[0].length;int maxArea = 0;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {maxArea = Math.max(maxArea, dfs(grid, i, j));}}return maxArea;}private int dfs(int[][] grid, int r, int c) {if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == 0) {return 0;}grid[r][c] = 0;int area = 1;for (int[] d : direction) {area += dfs(grid, r + d[0], c + d[1]);}return area;}
