代码随想录算法训练营第五十八天 | 101.孤岛的总面积、102.沉没孤岛、103.水流问题、104.建造最大岛屿、复习

101.孤岛的总面积

题目链接:https://kamacoder.com/problempage.php?pid=1173
文档讲解:https://programmercarl.com/kamacoder/0101.%E5%AD%A4%E5%B2%9B%E7%9A%84%E6%80%BB%E9%9D%A2%E7…

思路

本题要求找到不靠边的陆地面积,那么我们只要从周边找到陆地然后 通过 dfs或者bfs 将周边靠陆地且相邻的陆地都变成海洋,然后再去重新遍历地图 统计此时还剩下的陆地就可以了。

代码

import java.util.*;

class Main {
    static int[][] grid;
    static int count = 0;
    static int[][] dir = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), 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();
            }
        }
        
        for (int i = 0; i < n; i++) { // 从第一列和最后一列向中间遍历
            if (grid[i][0] == 1) bfs(i, 0);
            if (grid[i][m - 1] == 1) bfs(i, m - 1);
        }
        for (int i = 0; i < m; i++) { //从第一行和最后一行向中间遍历
            if (grid[0][i] == 1) bfs(0, i);
            if (grid[n - 1][i] == 1) bfs(n - 1, i);
        }
        count = 0; // 清空之前记录的陆地数
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    bfs(i, j);
                }
            } 
        }
        System.out.println(count);
    }
    
    public static void bfs(int x, int y) {
        count++;
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x, y});
        grid[x][y] = 0;
        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 (grid[nextX][nextY] == 1) {
                    queue.offer(new int[]{nextX, nextY});
                    grid[nextX][nextY] = 0;
                    count++;
                }
            }
        }
        
    }
    
    public static void dfs(int x, int y) {
        grid[x][y] = 0; // 将与边界相连的陆地变为水
        count++; // 统计陆地数量
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0], nextY = y + dir[i][1];
            if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
            if (grid[nextX][nextY] == 1) dfs(nextX, nextY);
        }
    }
}

这道题在卡码网上提交java的dfs和bfs都会超时,但是思路是和C++一样的。

102.沉没孤岛

题目链接:https://kamacoder.com/problempage.php?pid=1174
文档讲解:https://programmercarl.com/kamacoder/0102.%E6%B2%89%E6%B2%A1%E5%AD%A4%E5%B2%9B.html

思路

第一步:深搜或者广搜将地图周边的 1 (陆地)全部改成 2 (特殊标记)
第二步:将水域中间 1 (陆地)全部改成 水域(0)
第三步:将之前标记的 2 改为 1 (陆地)

代码

import java.util.*;

class Main{
    static int[][] grid;
    static int[][] dir = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), 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();
            }
        }
        
        for (int i = 0; i < n; i++) {
            if (grid[i][0] == 1) bfs(i, 0);
            if (grid[i][m - 1] == 1) bfs(i, m - 1);
        }
        for (int i = 0; i < m; i++) {
            if (grid[0][i] == 1) bfs(0, i);
            if (grid[n - 1][i] == 1) bfs(n - 1, i);
        }
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) grid[i][j] = 0;
                else if (grid[i][j] == 2) grid[i][j] = 1;
                System.out.print(grid[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    public static void bfs(int x, int y) {
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x, y});
        grid[x][y] = 2;
        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 (grid[nextX][nextY] == 1) {
                    grid[nextX][nextY] = 2;
                    queue.offer(new int[]{nextX, nextY});
                }
            }
        }
    }
    
    public static void dfs(int x, int y) {
        grid[x][y] = 2;
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0], nextY = y + dir[i][1];
            if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
            if (grid[nextX][nextY] == 1) dfs(nextX, nextY);
        }
    }
}

103.水流问题

题目链接:https://kamacoder.com/problempage.php?pid=1175
文档讲解:https://programmercarl.com/kamacoder/0103.%E6%B0%B4%E6%B5%81%E9%97%AE%E9%A2%98.html

思路

从第一组边界上的节点 逆流而上,将遍历过的节点都标记上。同样从第二组边界的边上节点 逆流而上,将遍历过的节点也标记上。然后两方都标记过的节点就是既可以流第一边界也可以流第二边界的节点。

代码

import java.util.*;

class Main {
    static int[][] grid;
    static int[][] dir = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
    
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), 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();
            }
        }
        boolean[][] visited1 = new boolean[n][m];
        boolean[][] visited2 = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            bfs(visited1, i, 0); // 最左列,为第一组
            bfs(visited2, i, m - 1); // 最右列,为第二组
        }
        for (int i = 0; i < m; i++) {
            bfs(visited1, 0, i); // 第一行,为第一组
            bfs(visited2, n - 1, i); // 第二行,为第二组
        }
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (visited1[i][j] && visited2[i][j]) {
                    System.out.println(i + " " + j);
                }
            }
        }
    }
    
    public static void bfs(boolean[][] visited, int x, int y) {
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x, y});
        visited[x][y] = true;
        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[curX][curY] <= grid[nextX][nextY]) {
                    queue.offer(new int[]{nextX, nextY});
                    visited[nextX][nextY] = true;
                }
            }
        }
    }
    
    public static void dfs(boolean[][] visited, int x, int y) {
        visited[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0], nextY = y + dir[i][1];
            if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
            if (!visited[nextX][nextY] && grid[x][y] <= grid[nextX][nextY]) dfs(visited, nextX, nextY);
        }
    }
}

104.建造最大岛屿

题目链接:https://kamacoder.com/problempage.php?pid=1176
文档讲解:https://programmercarl.com/kamacoder/0104.%E5%BB%BA%E9%80%A0%E6%9C%80%E5%A4%A7%E5%B2%9B%E5…

思路

第一步:一次遍历地图,得出各个岛屿的面积,并做编号记录。可以使用map记录,key为岛屿编号,value为岛屿面积
第二步:再遍历地图,遍历0的方格(因为要将0变成1),并统计该1(由0变成的1)周边岛屿面积,将其相邻面积相加在一起,遍历所有 0 之后,就可以得出 选一个0变成1 之后的最大面积。

代码

import java.util.*;
class Main {
    static int n, m;
    static int[][] grid;
    static int[][] markGrid;
    static boolean[][] visited;
    static int count;
    static int[][] dir = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
    static HashMap<Integer, Integer> map = new HashMap<>();
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        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();
            }
        }
        markGrid = new int[n][m];
        visited = new boolean[n][m];
        
        // 将岛屿编号,并把对应的面积存入map中
        int mark = 2;
        boolean isAllLand = true;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 0) isAllLand = false;
                if (!visited[i][j] && grid[i][j] == 1) {
                    count = 0;
                    dfs(i, j, mark);
                    map.put(mark, count);
                    mark++;
                }
            }
        }
        if (isAllLand) System.out.println(n * m);// 如果全是陆地,则直接返回面积n*m
        else { // 遍历grid值为0的节点,将其四边对应编号的面积相加
            int res = 0;
            // 用set来过滤已经添加过的mark值,防止遍历到同一编号的陆地导致重复添加
            Set<Integer> set = new HashSet<>(); 
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    count = 1;
                    set.clear();
                    if (grid[i][j] == 0) {
                        for (int k = 0; k < 4; k++) {
                            int nextX = i + dir[k][0], nextY = j + dir[k][1];
                            if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= m) continue;
                            if (set.contains(grid[nextX][nextY])) continue; // 添加过的岛屿跳过
                            count += map.getOrDefault(grid[nextX][nextY], 0);
                            set.add(grid[nextX][nextY]);
                        }
                    }
                    res = Math.max(res, count);
                }
            }
            System.out.println(res);
        }
        
    }
    
    public static void dfs(int x, int y, int mark) {
        visited[x][y] = true;
        grid[x][y] = mark;
        count++;
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0], nextY = y + dir[i][1];
            if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= m) continue;
            if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) dfs(nextX, nextY, mark);
        }
    }
}

复习二叉树

二叉树理论基础篇
二叉树的递归遍历
二叉树的迭代遍历
二叉树的统一迭代法
二叉树层序遍历

对应题目:

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值