Day52 >> 101、孤岛的总面积 + 102、沉默孤岛 + 103、水流问题 + 104、建造最大岛屿
代码随想录-图论Part3101、孤岛的总面积package test.java; import java.util.*; public class dfsPart3 { private static int count 0; private static final int[][] dir {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; private static void bfs(int[][] grid, int x, int y) { Queueint[] que new LinkedList(); que.add(new int[]{x, y}); grid[x][y] 0; // 只要加入队列立刻标记 count; while (!que.isEmpty()) { int[] cur que.poll(); 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 (grid[nextx][nexty] 1) { que.add(new int[]{nextx, nexty}); count; grid[nextx][nexty] 0; // 只要加入队列立刻标记 } } } } public static void main(String[] args) { Scanner scanner new Scanner(System.in); int n scanner.nextInt(); int m scanner.nextInt(); int[][] grid new int[n][m]; // 读取网格 for (int i 0; i n; i) { for (int j 0; j m; j) { grid[i][j] scanner.nextInt(); } } // 从左侧边和右侧边向中间遍历 for (int i 0; i n; i) { if (grid[i][0] 1) bfs(grid, i, 0); if (grid[i][m - 1] 1) bfs(grid, i, m - 1); } // 从上边和下边向中间遍历 for (int j 0; j m; j) { if (grid[0][j] 1) bfs(grid, 0, j); if (grid[n - 1][j] 1) bfs(grid, n - 1, j); } count 0; for (int i 0; i n; i) { for (int j 0; j m; j) { if (grid[i][j] 1) bfs(grid, i, j); } } System.out.println(count); scanner.close(); } }102、沉默孤岛package test.java; import java.util.Scanner; public class dfsPart4 { static int[][] dir { {-1, 0}, {0, -1}, {1, 0}, {0, 1} }; // 保存四个方向 public static void dfs(int[][] grid, int x, int y) { grid[x][y] 2; for (int[] d : dir) { int nextX x d[0]; int nextY y d[1]; // 超过边界 if (nextX 0 || nextX grid.length || nextY 0 || nextY grid[0].length) continue; // 不符合条件不继续遍历 if (grid[nextX][nextY] 0 || grid[nextX][nextY] 2) continue; dfs(grid, nextX, nextY); } } public static void main(String[] args) { Scanner scanner new Scanner(System.in); int n scanner.nextInt(); int m scanner.nextInt(); int[][] grid new int[n][m]; for (int i 0; i n; i) { for (int j 0; j m; j) { grid[i][j] scanner.nextInt(); } } // 步骤一 // 从左侧边和右侧边 向中间遍历 for (int i 0; i n; i) { if (grid[i][0] 1) dfs(grid, i, 0); if (grid[i][m - 1] 1) dfs(grid, i, m - 1); } // 从上边和下边 向中间遍历 for (int j 0; j m; j) { if (grid[0][j] 1) dfs(grid, 0, j); if (grid[n - 1][j] 1) dfs(grid, n - 1, j); } // 步骤二、步骤三 for (int i 0; i n; i) { for (int j 0; j m; j) { if (grid[i][j] 1) grid[i][j] 0; if (grid[i][j] 2) grid[i][j] 1; } } for (int i 0; i n; i) { for (int j 0; j m; j) { System.out.print(grid[i][j] ); } System.out.println(); } scanner.close(); } }103、水流问题这道太难了……后续再练习吧104、建造最大岛屿package test.java; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class dfsPart5 { // 该方法采用 DFS // 定义全局变量 // 记录每次每个岛屿的面积 static int count; // 对每个岛屿进行标记 static int mark; // 定义二维数组表示四个方位 static int[][] dirs {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // DFS 进行搜索将每个岛屿标记为不同的数字 public static void dfs(int[][] grid, int x, int y, boolean[][] visited) { // 当遇到边界直接return if (x 0 || x grid.length || y 0 || y grid[0].length) return; // 遇到已经访问过的或者遇到海水直接返回 if (visited[x][y] || grid[x][y] 0) return; visited[x][y] true; count; grid[x][y] mark; // 继续向下层搜索 dfs(grid, x, y 1, visited); dfs(grid, x, y - 1, visited); dfs(grid, x 1, y, visited); dfs(grid, x - 1, y, visited); } 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(); } } // 初始化mark变量从2开始区别于0水1岛屿 mark 2; // 定义二位boolean数组记录该位置是否被访问 boolean[][] visited new boolean[m][n]; // 定义一个HashMap记录某片岛屿的标记号和面积 HashMapInteger, Integer getSize new HashMap(); // 定义一个HashSet用来判断某一位置水四周是否存在不同标记编号的岛屿 HashSetInteger set new HashSet(); // 定义一个boolean变量看看DFS之后是否全是岛屿 boolean isAllIsland true; // 遍历二维数组进行DFS搜索标记每片岛屿的编号记录对应的面积 for (int i 0; i m; i) { for (int j 0; j n; j) { if (grid[i][j] 0) isAllIsland false; if (grid[i][j] 1) { count 0; dfs(grid, i, j, visited); getSize.put(mark, count); mark; } } } int result 0; if (isAllIsland) result m * n; // 对标记完的grid继续遍历判断每个水位置四周是否有岛屿并记录下四周不同相邻岛屿面积之和 // 每次计算完一个水位置周围可能存在的岛屿面积之和更新下result变量 for (int i 0; i m; i) { for (int j 0; j n; j) { if (grid[i][j] 0) { set.clear(); // 当前水位置变更为岛屿所以初始化为1 int curSize 1; for (int[] dir : dirs) { int curRow i dir[0]; int curCol j dir[1]; if (curRow 0 || curRow m || curCol 0 || curCol n) continue; int curMark grid[curRow][curCol]; // 如果当前相邻的岛屿已经遍历过或者HashMap中不存在这个编号继续搜索 if (set.contains(curMark) || !getSize.containsKey(curMark)) continue; set.add(curMark); curSize getSize.get(curMark); } result Math.max(result, curSize); } } } System.out.println(result); sc.close(); } }

相关新闻

快捷键:Ctrl+Shift+P打开命令面板

快捷键:Ctrl+Shift+P打开命令面板

2026/5/17 0:18:03 阅读更多 →
内存-磁盘

内存-磁盘

2026/5/17 0:18:03 阅读更多 →
硬件异构性-cpu-gpu-npu

硬件异构性-cpu-gpu-npu

2026/7/2 22:40:38 阅读更多 →

最新新闻

如何高效跳过FF14副本动画:30分钟掌握智能插件实战指南

如何高效跳过FF14副本动画:30分钟掌握智能插件实战指南

如何高效跳过FF14副本动画:30分钟掌握智能插件实战指南 【免费下载链接】FFXIV_ACT_CutsceneSkip 项目地址: https://gitcode.com/gh_mirrors/ff/FFXIV_ACT_CutsceneSkip 想象一下这样的场景:你正沉浸在《最终幻想14》的副本挑战中,团…

2026/7/3 10:43:26 阅读更多 →
5个步骤让你的普通鼠标在macOS上获得苹果触控板般的流畅体验

5个步骤让你的普通鼠标在macOS上获得苹果触控板般的流畅体验

5个步骤让你的普通鼠标在macOS上获得苹果触控板般的流畅体验 【免费下载链接】mac-mouse-fix Mac Mouse Fix - Make Your $10 Mouse Better Than an Apple Trackpad! 项目地址: https://gitcode.com/GitHub_Trending/ma/mac-mouse-fix 你是否在macOS上使用第三方鼠标时感…

2026/7/3 10:41:25 阅读更多 →
构建 AI Agent 应该优先设计路由,把模型选型留到最后。Tom Tunguz 谏言。

构建 AI Agent 应该优先设计路由,把模型选型留到最后。Tom Tunguz 谏言。

在 2026 年的今天,如果你去翻看各大技术团队构建 AI 智能体(Agent)的架构设计文档,你会发现一个非常普遍的“反向骚操作”:绝大多数团队都是先敲定用哪个大模型(比如非 GPT-5.5 或 Claude 4.8 不选&#xf…

2026/7/3 10:41:25 阅读更多 →
Adobe软件快速激活终极指南:3分钟解锁Photoshop等全套专业工具

Adobe软件快速激活终极指南:3分钟解锁Photoshop等全套专业工具

Adobe软件快速激活终极指南:3分钟解锁Photoshop等全套专业工具 【免费下载链接】Adobe-GenP Adobe CC 2019/2020/2021/2022/2023 GenP Universal Patch 3.0 项目地址: https://gitcode.com/gh_mirrors/ad/Adobe-GenP 想要免费使用Adobe Creative Cloud中的专…

2026/7/3 10:35:21 阅读更多 →
WS2812与GD32VF103VBT6实现动态光效系统开发指南

WS2812与GD32VF103VBT6实现动态光效系统开发指南

1. 项目概述:用WS2812与GD32VF103VBT6打造动态光效系统最近在工作室折腾LED灯带时,发现WS2812智能灯珠和GD32VF103VBT6这款RISC-V开发板简直是绝配。WS2812作为市面上最流行的可寻址RGB LED,每个像素点都能独立控制;而GD32VF103VB…

2026/7/3 10:33:20 阅读更多 →
Java面试常见误区与高效复习方法

Java面试常见误区与高效复习方法

很多Java求职者面试失败,不是因为不努力,而是因为努力的方向错了。你以为刷完两百道“八股文”就能拿下Offer?实际上面试官随便问一句“HashMap的扩容机制为什么是2的幂次方”就能让你卡壳。真正的复习,不是把知识点装进口袋&…

2026/7/3 10:29:19 阅读更多 →

日新闻

Nginx防御TLS重协商攻击实战:从原理到配置与监控

Nginx防御TLS重协商攻击实战:从原理到配置与监控

1. 项目概述:为什么TLS重协商攻击至今仍需警惕十多年前的CVE-2011-1473,一个关于TLS/SSL协议重协商机制的漏洞,现在提起来还有必要吗?很多运维和开发朋友可能会觉得,这都老掉牙了,现代服务器和客户端不都默…

2026/7/3 0:03:59 阅读更多 →
华为防火墙双通道远程管理实战:Web与SSH配置详解

华为防火墙双通道远程管理实战:Web与SSH配置详解

1. 项目概述:为什么需要双通道远程管理防火墙?在任何一个稍具规模的企业网络里,防火墙都是那个默默守护在边界的关键角色。作为网络工程师,我们不可能每次都跑到机房,插上console线去配置它。远程管理能力,…

2026/7/3 0:03:59 阅读更多 →
AD74413R与PIC18F65K40的高精度工业数据采集方案

AD74413R与PIC18F65K40的高精度工业数据采集方案

1. 项目概述:AD74413R与PIC18F65K40的协同工作在工业自动化和精密测量领域,同时实现高精度模数转换(ADC)和数模转换(DAC)功能是许多复杂系统的核心需求。AD74413R作为一款四通道可配置模拟输入/输出器件,与PIC18F65K40微控制器的组合&#xf…

2026/7/3 0:05:59 阅读更多 →

周新闻

月新闻