岛屿系列DFS框架
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8f88a862fac2443ee9ae2c3578b0d1d7.png)
L200. 岛屿数量 (Easy)
class Solution {
public int numIslands(char[][] grid) {
int num = 0 ;
for(int i = 0 ; i < grid.length ; i++){
for(int j = 0 ; j < grid[0].length ; j++){
if(grid[i][j] == '1'){
becomeZero(grid, i, j,num);
num++;
}
}
}
return num;
}
public void becomeZero(char[][] grid, int i, int j, int num){
int nr = grid.length;
int nc = grid[0].length;
if (i < 0 || j < 0 || i >= nr || j >= nc || grid[i][j] == '0') {
return;
}
grid[i][j] = '0';
becomeZero(grid, i+1, j, num);
becomeZero(grid, i-1, j , num);
becomeZero(grid, i, j+1 , num);
becomeZero(grid, i, j-1 , num);
}
}
463. 岛屿的周长 (Easy)
class Solution {
public int islandPerimeter(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
int res = 0;
for(int i = 0; i < n ; i++){
for(int j = 0 ; j < m; j++){
if(grid[i][j] == 1){
res = dfs(grid,i,j);
}
}
}
return res;
}
public int dfs(int[][] grid, int i,int j){
if(! panduan(grid,i,j)){
return 1;
}
if(grid[i][j] == 0){
return 1;
}
if(grid[i][j] != 1){
return 0;
}
grid[i][j] = 2;
return ( dfs(grid,i-1,j)+dfs(grid,i+1,j)+dfs(grid,i,j-1)+dfs(grid,i,j+1) );
}
public boolean panduan(int[][] grid, int i , int j){
return ( ((i >= 0) && (i < grid.length)) && ((j >= 0) && (j < grid[0].length)) );
}
}
695. 岛屿的最大面积 (Medium)
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
int res = 0;
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < m ; j++){
if(grid[i][j] == 1){
res = Math.max(dfs(grid,i,j) , res);
}
}
}
return res;
}
public int dfs(int[][] grid , int r, int s){
if(!panduan(grid, r, s)){
return 0;
}
if(grid[r][s] != 1){
return 0;
}
grid[r][s] = 2;
return 1 + dfs(grid, r+1, s)
+ dfs(grid, r-1, s)
+ dfs(grid, r, s+1)
+ dfs(grid, r, s-1);
}
public boolean panduan(int[][] grid, int r, int s){
return ( (0<= r)&& (r < grid.length)) && ( (0<= s)&& (s < grid[0].length) );
}
}
827. 最大人工岛 (Hard)
import java.util.Hashtable;
class Solution {
public static int largestIsland(int[][] grid) {
Hashtable<Integer,Integer> ht = new Hashtable<>(); //存放index和面积
int n = grid.length;
int m = grid[0].length;
int res = 0;
int index = 2;
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < m ; j++){
if(grid[i][j] == 1){
int temp = dfs(grid,i,j,index); //岛屿面积
//grid = dfsIndex(grid,i,j,index); //矩阵
ht.put(index,temp);
index++;
res = Math.max(res,temp);
}
}
}
if(res == 0 ) return 1 ;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 0) {
HashSet<Integer> hs = neighbor(grid ,i, j);
if(hs.size() < 1) continue;
int count = 1; //海洋变成陆地的1
for(int k : hs){
count = ht.get(k) + count;
res = Math.max(count,res);
}
}
}
}
return res;
}
public static HashSet<Integer> neighbor(int[][] grid ,int r, int s) {
HashSet<Integer> hs = new HashSet<>();
if(panduan(grid,r,s-1) && grid[r][s-1] != 0){
hs.add(grid[r][s-1]);
}
if(panduan(grid,r,s+1) && grid[r][s+1] != 0){
hs.add(grid[r][s+1]);
}
if(panduan(grid,r-1,s) && grid[r-1][s] != 0){
hs.add(grid[r-1][s]);
}
if(panduan(grid,r+1,s) && grid[r+1][s] != 0){
hs.add(grid[r+1][s]);
}
return hs;
}
public static int dfs(int[][] grid , int r, int s , int index){
if(!panduan(grid, r, s)){
return 0;
}
if(grid[r][s] != 1){
return 0;
}
grid[r][s] = index;
return 1 + dfs(grid, r+1, s ,index)
+ dfs(grid, r-1, s,index)
+ dfs(grid, r, s+1,index)
+ dfs(grid, r, s-1,index);
}
//将岛屿赋值为2,3,4...
// public static int[][] dfsIndex(int[][] grid , int r, int s, int index ){
// if(!panduan(grid, r, s)){
// return grid;
// }
// if(grid[r][s] != 1){
// return grid;
// }
// grid[r][s] = index;
// dfsIndex(grid, r+1, s , index);
// dfsIndex(grid, r-1, s , index);
// dfsIndex(grid, r, s+1, index);
// dfsIndex(grid, r, s-1, index);
// return grid;
// }
public static boolean panduan(int[][] grid, int r, int s){
return ( (0<= r)&& (r < grid.length)) && ( (0<= s)&& (s < grid[0].length) );
}
}