代码随想录算法训练营第五十七天 | 99.岛屿数量 深搜/广搜、100.岛屿的最大面积

99.岛屿数量 深搜/广搜

题目链接:https://kamacoder.com/problempage.php?pid=1171
文档讲解:深搜广搜

思路

遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。

代码

import java.util.*;

class Main {
    static int[][] grid;
    static boolean[][] visited;
    static int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        grid = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                grid[i][j] = in.nextInt();
            }
        }
        visited = new boolean[n][m];
        int res = 0;
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!visited[i][j] && grid[i][j] == 1) {
                    res++;
                    visited[i][j] = true;
                    // dfs(i, j);
                    bfs(i, j);
                }
            }
        }
        System.out.println(res);
    }
    
    public static void bfs(int x, int y) {
        Deque<int[]> deque = new LinkedList<>();
        deque.push(new int[]{x, y});
        visited[x][y] = true;
        while (!deque.isEmpty()) {
            int[] cur = deque.pop();
            int curX = cur[0];
            int curY = cur[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) {
                    deque.push(new int[]{nextX, nextY});
                    visited[nextX][nextY] = true; // 所以只要加入队列,立即标记该节点走过。否则会超时
                }
            }
        }
    }
    
    public static void dfs(int x, int y) {
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
            if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
                visited[nextX][nextY] = true;
                dfs(nextX, nextY);
            }
        }
    }
}

100.岛屿的最大面积

题目链接:https://kamacoder.com/problempage.php?pid=1172
文档讲解:https://programmercarl.com/kamacoder/0100.%E5%B2%9B%E5%B1%BF%E7%9A%84%E6%9C%80%E5%A4%A7%E9…

思路

搜索每个岛屿上“1”的数量,然后取一个最大的。

代码

import java.util.*;

class Main{
    static int[][] grid;
    static boolean[][] visited;
    static int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    static int count;
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        grid = new int[n][m];
        visited = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                grid[i][j] = in.nextInt();
            }
        }
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!visited[i][j] && grid[i][j] == 1) {
                    count = 1;
                    visited[i][j] = true;
                    //dfs(i, j);
                    bfs(i, j);
                    res = Math.max(res, count);
                }
            }
        }
        System.out.println(res);
    }
    
    public static void bfs(int x, int y) {
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x, y});
        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int curX = cur[0], curY = cur[1];
            for (int i = 0; i < 4; i++) {
                int nextX = curX + dir[i][0], 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) {
                    count++;
                    queue.offer(new int[]{nextX, nextY});
                    visited[nextX][nextY] = true;
                }
            }
        }
    }
    
    public static void dfs(int x, int y) {
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
            if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
                count++;
                visited[nextX][nextY] = true;
                dfs(nextX, nextY);
            }
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值