DFS专题:力扣岛屿问题
开篇
每次做到DFS相关的题目都是直接跳过。蓝桥杯过后痛定思痛,好好学习一下DFS和BFS。先从DFS开始吧。
参考题解:nettee:岛屿类问题的通用解法、DFS 遍历框架
一、岛屿数量
题目链接: 200.岛屿数量
题目描述
代码思路
使用for对每一个网格点进行判断,如果遇到未搜索过的’1’,则使岛屿数加一,并利用dfs将与其相连的‘1’都进行标记,确保每次搜索到1都是一个新的岛屿。
代码纯享版
class Solution {public int numIslands(char[][] grid) {int len = grid.length; int wide = grid[0].length; int sum = 0;for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){if(grid[i][j] == '1'){sum++;dfs(grid, i, j);}}}return sum;}void dfs(char[][] grid, int i, int j){int len = grid.length;int wide = grid[0].length;if(i < 0 || i >= len || j < 0 || j >= wide || grid[i][j] != '1'){return;}grid[i][j] = '2'; dfs(grid, i - 1, j);dfs(grid, i + 1, j);dfs(grid, i, j - 1);dfs(grid, i, j + 1);}
}
代码逐行解析版
class Solution {public int numIslands(char[][] grid) {int len = grid.length; //岛屿长度int wide = grid[0].length; //岛屿宽度int sum = 0; //岛屿总数//遍历岛屿每一个位置for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){ if(grid[i][j] == '1'){ //如果为陆地sum++; //岛屿总数加一dfs(grid, i, j); //dfs}}}return sum;}//深度搜索所有连接在一起的'1',将其变为'2'//grid[i][j] == '0' 水//grid[i][j] == '1' 未搜索的陆地//grid[i][j] == '2' 已搜索的陆地void dfs(char[][] grid, int i, int j){int len = grid.length;int wide = grid[0].length;//排除2种情况//1.超出网格范围的搜索//2.不是未搜索的陆地if(i < 0 || i >= len || j < 0 || j >= wide || grid[i][j] != '1'){ return;}grid[i][j] = '2'; //将grid[i][j] == '1'的情况标记为'2'//上下左右四个方位的搜索dfs(grid, i - 1, j);dfs(grid, i + 1, j);dfs(grid, i, j - 1);dfs(grid, i, j + 1);}
}
二、岛屿的周长
题目链接: 463.岛屿的周长
题目描述
代码思路
利用for循环遍历到一块陆地后,对这块陆地进行dfs搜索,遇到海洋或超出区域的部分,则周长加一;遇到未搜索过的陆地,则标记为搜索过;遇到搜索过的陆地,直接返回。
代码纯享版
class Solution {public int area = 0;public int islandPerimeter(int[][] grid) {int len = grid.length;int wide = grid[0].length;int i = 0, j = 0;int judge = 1;for(i = 0; i < len; i++){for(j = 0; j < wide; j++){if(grid[i][j] == 1){dfs(grid, i , j);return area;}}}return 0;}void dfs(int[][] grid, int i, int j){int len = grid.length;int wide = grid[0].length;if(i < 0 || i >= len || j < 0 || j >= wide || grid[i][j] == 0){area++;return;}if(grid[i][j] == 2){return;}grid[i][j] = 2;dfs(grid, i - 1, j);dfs(grid, i + 1, j);dfs(grid, i, j + 1);dfs(grid, i, j - 1);}
}
代码逐行解析版
class Solution {public int area = 0; //设周长为公共变量public int islandPerimeter(int[][] grid) {int len = grid.length; int wide = grid[0].length;for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){if(grid[i][j] == 1){ //碰到陆地,进行dfsdfs(grid, i , j); return area; //只有一块岛屿,所以搜索一次后即可返回周长}}}return 0; //没找到岛屿,返回0}void dfs(int[][] grid, int i, int j){int len = grid.length;int wide = grid[0].length;//由题目图可知,搜索到超出区域范围或海洋位置的,周长加一if(i < 0 || i >= len || j < 0 || j >= wide || grid[i][j] == 0){ area++;return;}if(grid[i][j] == 2){ //已搜索过的直接返回return;}grid[i][j] = 2; //把未搜索过的陆地标记为2:已搜索过的陆地//对上下左右进行搜索dfs(grid, i - 1, j);dfs(grid, i + 1, j);dfs(grid, i, j + 1);dfs(grid, i, j - 1);}
}
大佬相似题解
主要区别是没有设置周长变量为公共变量,而是在递归中用return来返回计算结果,感觉更高级
public int islandPerimeter(int[][] grid) {for (int r = 0; r < grid.length; r++) {for (int c = 0; c < grid[0].length; c++) {if (grid[r][c] == 1) {// 题目限制只有一个岛屿,计算一个即可return dfs(grid, r, c);}}}return 0;
}int dfs(int[][] grid, int r, int c) {// 函数因为「坐标 (r, c) 超出网格范围」返回,对应一条黄色的边if (!inArea(grid, r, c)) {return 1;}// 函数因为「当前格子是海洋格子」返回,对应一条蓝色的边if (grid[r][c] == 0) {return 1;}// 函数因为「当前格子是已遍历的陆地格子」返回,和周长没关系if (grid[r][c] != 1) {return 0;}grid[r][c] = 2;return dfs(grid, r - 1, c)+ dfs(grid, r + 1, c)+ dfs(grid, r, c - 1)+ dfs(grid, r, c + 1);
}// 判断坐标 (r, c) 是否在网格中
boolean inArea(int[][] grid, int r, int c) {return 0 <= r && r < grid.length && 0 <= c && c < grid[0].length;
}
三、岛屿的最大面积
题目链接: 695.岛屿的最大面积
题目描述
代码思路
利用for循环遍历到一块陆地后,对这块陆地进行dfs搜索,遇到海洋或超出区域的部分,则周长加一;遇到未搜索过的陆地,则标记为搜索过;遇到搜索过的陆地,直接返回。
代码纯享版
class Solution {public int area = 0;public int maxAreaOfIsland(int[][] grid) {int len = grid.length;int wide = grid[0].length;int max = 0; for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){if(grid[i][j] == 1){dfs(grid, i, j);max = Math.max(max, area);area = 0;}}}return max;}void dfs(int[][] grid, int i, int j){int len = grid.length;int wide = grid[0].length;if(i < 0 || i >= len || j < 0 || j >= wide || grid[i][j] != 1){return;}area++;grid[i][j] = 2;dfs(grid, i - 1, j);dfs(grid, i + 1, j);dfs(grid, i, j - 1);dfs(grid, i, j + 1);}
}
代码逐行解析版
class Solution {public int area = 0; //设岛屿的面积为公共变量public int maxAreaOfIsland(int[][] grid) {int len = grid.length;int wide = grid[0].length;int max = 0; //用来统计岛屿最大面积for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){ //遍历每一个单元格if(grid[i][j] == 1){ //遇到土地dfs(grid, i, j); //进行dfsmax = Math.max(max, area); //获取岛屿面积最大值area = 0; //将area重新设为0,方便下一块岛屿面积的计算}}}return max; //返回岛屿面积最大值}void dfs(int[][] grid, int i, int j){int len = grid.length;int wide = grid[0].length;//遇到超出的区域或水,直接返回if(i < 0 || i >= len || j < 0 || j >= wide || grid[i][j] != 1){return;}area++; //面积加一grid[i][j] = 2; //标记为搜索过//对上下左右区域进行搜索dfs(grid, i - 1, j);dfs(grid, i + 1, j);dfs(grid, i, j - 1);dfs(grid, i, j + 1);}
}
大佬相似题解
主要区别是没有设置周长变量为公共变量,而是在递归中用return来返回计算面积结果
public int maxAreaOfIsland(int[][] grid) {int res = 0;for (int r = 0; r < grid.length; r++) {for (int c = 0; c < grid[0].length; c++) {if (grid[r][c] == 1) {int a = area(grid, r, c);res = Math.max(res, a);}}}return res;
}int area(int[][] grid, int r, int c) {if (!inArea(grid, r, c)) {return 0;}if (grid[r][c] != 1) {return 0;}grid[r][c] = 2;return 1 + area(grid, r - 1, c)+ area(grid, r + 1, c)+ area(grid, r, c - 1)+ area(grid, r, c + 1);
}boolean inArea(int[][] grid, int r, int c) {return 0 <= r && r < grid.length && 0 <= c && c < grid[0].length;
}
四、最大人工岛
题目链接: 827.最大人工岛
题目描述
代码思路
这道题作为困难题,比前面三道复杂一点,要进行两次搜索。第一次是利用dfs,统计每一块岛屿的面积。第二次是寻找为0的节点,计算其四个方位岛屿的面积。
代码纯享版
class Solution {public static int area = 0; //面积public static Map<Integer, Integer> map = new HashMap(); //记录每块岛屿记号及对应面积public static int sign = 2; //从2开始标记public int largestIsland(int[][] grid) {int len = grid.length;int wide = grid[0].length;int max = 0;for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){if(grid[i][j] == 1){dfs(grid, i, j);map.put(sign, area);sign++;area = 0;}}}for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){if(grid[i][j] == 0){Set<Integer> set = new HashSet();if(i + 1 < len){area += map.getOrDefault(grid[i + 1][j], 0);set.add(grid[i + 1][j]); }if(i - 1 >= 0 && !set.contains(grid[i - 1][j])){area += map.getOrDefault(grid[i - 1][j], 0);set.add(grid[i - 1][j]);}if(j + 1 < wide && !set.contains(grid[i][j + 1])){area += map.getOrDefault(grid[i][j + 1], 0);set.add(grid[i][j + 1]);}if(j - 1 >= 0 && !set.contains(grid[i][j - 1])){area += map.getOrDefault(grid[i][j - 1], 0);set.add(grid[i][j - 1]);}max = Math.max(max, area + 1);area = 0;}}}return max == 0 ? len * wide : max;}void dfs(int[][] grid, int i, int j){int len = grid.length;int wide = grid[0].length;if(i < 0 || i >= len || j < 0 || j >= wide || grid[i][j] != 1){return;}area++;grid[i][j] = sign;dfs(grid, i + 1, j);dfs(grid, i - 1, j);dfs(grid, i, j + 1);dfs(grid, i, j - 1);}
}
代码逐行解析版
class Solution {public static int area = 0; //面积public static Map<Integer, Integer> map = new HashMap(); //记录每块岛屿记号及对应面积public static int sign = 2; //从2开始标记public int largestIsland(int[][] grid) {int len = grid.length;int wide = grid[0].length;int max = 0; //记录面积最大值//第一步:利用dfs统计每一块岛屿的面积for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){ //遍历每一个点if(grid[i][j] == 1){ //遇到岛屿点dfs(grid, i, j); //通过dfs计算岛屿面积map.put(sign, area); //用map来标记岛屿序号并记录对应面积sign++;area = 0; //将面积变成0,方便下一个岛屿面积的统计}}}//第二步:找到每一个为0的点,计算其上下左右四个方位的岛屿面积和for(int i = 0; i < len; i++){for(int j = 0; j < wide; j++){if(grid[i][j] == 0){ //遇到为0的点Set<Integer> set = new HashSet(); //利用集合防止重复计算同一片岛屿//对四个方向的岛屿面积进行相加if(i + 1 < len){area += map.getOrDefault(grid[i + 1][j], 0);set.add(grid[i + 1][j]); }if(i - 1 >= 0 && !set.contains(grid[i - 1][j])){area += map.getOrDefault(grid[i - 1][j], 0);set.add(grid[i - 1][j]);}if(j + 1 < wide && !set.contains(grid[i][j + 1])){area += map.getOrDefault(grid[i][j + 1], 0);set.add(grid[i][j + 1]);}if(j - 1 >= 0 && !set.contains(grid[i][j - 1])){area += map.getOrDefault(grid[i][j - 1], 0);set.add(grid[i][j - 1]);}max = Math.max(max, area + 1); //取最大值,area+1的加一是将grid[i][j]=0变成1area = 0;}}}//如果找不到为0的单元格,证明该矩阵全部都是1,直接返回矩阵面积,否则返回maxreturn max == 0 ? len * wide : max; }void dfs(int[][] grid, int i, int j){int len = grid.length;int wide = grid[0].length;//排除超出区域的情况和已统计过的情况if(i < 0 || i >= len || j < 0 || j >= wide || grid[i][j] != 1){ return;}area++; //面积加一grid[i][j] = sign; //标记岛屿记号,也使其被标记为统计过//上下左右四个方位进行搜索dfs(grid, i + 1, j);dfs(grid, i - 1, j);dfs(grid, i, j + 1);dfs(grid, i, j - 1);}
}