代码随想录算法训练营 | 岛屿数量 深搜,岛屿数量 广搜,岛屿的最大面积

岛屿数量 深搜
题目链接:岛屿数量 深搜
文档讲解︰代码随想录(programmercarl.com)
日期:2024-10-23

想法:
Java代码如下:

import java.util.Scanner;

public class Main {
    public static int[][] dir ={{0, 1},{1, 0},{-1, 0},{0, -1}};
    public static void dfs(boolean[][] visited,int x,int y ,int [][]grid)
    {
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            if(nextY < 0 || nextX < 0|| nextX >= grid.length || nextY >= grid[0].length)
                continue;
            if(!visited[nextX][nextY] && grid[nextX][nextY] == 1)
            {
                visited[nextX][nextY] = true;
                dfs(visited,nextX,nextY,grid);
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] grid = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                grid[i][j] = sc.nextInt();
            }
        }
        boolean[][]visited = new boolean[m][n];
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(!visited[i][j]&&grid[i][j] == 1)
                {
                    res++;
                    visited[i][j] = true;
                    dfs(visited,i,j,grid);
                }
            }
        }
        System.out.println(res);
    }
}

岛屿数量 广搜
题目链接:岛屿数量 广搜
文档讲解︰代码随想录(programmercarl.com)
日期:2024-10-23

想法:
Java代码如下:

import java.util.*;

public class Main {
    public static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    public static void bfs(int[][] grid, boolean[][] visited, int x, int y) {
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{x, y});
        visited[x][y] = true;
        while (!queue.isEmpty()) {
            int[] node = queue.poll();
            int curX = node[0];
            int curY = node[1];
            for (int i = 0; i < 4; i++) {
                int nextX = curX + dir[i][0];
                int nextY = curY + dir[i][1];
                if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) {
                    continue;
                }
                if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
                    queue.add(new int[]{nextX, nextY});
                    visited[nextX][nextY] = true;
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] grid = new int[m][n];
        boolean[][] visited = new boolean[m][n];
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                grid[i][j] = sc.nextInt();
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!visited[i][j] && grid[i][j] == 1) {
                    res++;
                    bfs(grid, visited, i, j);
                }
            }
        }
        System.out.println(res);
    }
}

岛屿的最大面积
题目链接:岛屿的最大面积
文档讲解︰代码随想录(programmercarl.com)
日期:2024-10-23

想法:
Java代码如下:

import java.util.*;

public class Main {
    public static int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    public static boolean[][] visited;
    public static int cnt = 0;
    public static void dfs(int[][] grid, int i, int j) {
        if (grid[i][j] == 0 || visited[i][j] == true) return;
        visited[i][j] = true;
        cnt++;
        for (int[] d : dir) {
            int r = i + d[0];
            int c = j + d[1];
            
            if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length)
                continue;
            
            dfs(grid, r, c);
        }
    }
    
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] grid = new int[m][n];
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                grid[i][j] = sc.nextInt();
            }
        }
        
        visited = new boolean[m][n];
        int max = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1 && visited[i][j] == false) {
                    cnt = 0;
                    dfs(grid, i, j);
                    max = Math.max(max, cnt);
                }
            }
        }
        
        System.out.println(max);
    }
}
posted @ 2024-10-23 20:35  漪欢酒  阅读(12)  评论(0编辑  收藏  举报