当前位置: 首页 > news >正文

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

101. 孤岛的总面积

题目链接:KamaCoder
文档讲解:代码随想录
状态:AC


Java代码:

import java.util.*;class Main {static int count = 0;static int res = 0;static boolean island = true;public static int[][] dir = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};public static void main(String[] args) {Scanner scan = new Scanner(System.in);int n = scan.nextInt();int m = scan.nextInt();int[][] arr = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {arr[i][j] = scan.nextInt();}}scan.close();boolean[][] visited = new boolean[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (arr[i][j] == 1 && !visited[i][j]) {visited[i][j] = true;count++;dfs(arr, visited, i, j);if (island) {res += count;}island = true;count = 0;}}}System.out.println(res);}public static void dfs(int[][] arr, boolean[][] visited, 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 || nextY < 0 || nextX >= arr.length || nextY >= arr[0].length) {island = false;continue;}if (arr[nextX][nextY] == 1 && !visited[nextX][nextY]) {visited[nextX][nextY] = true;count++;dfs(arr, visited, nextX, nextY);}}}
}

102. 沉没孤岛

题目链接:KamaCoder
文档讲解:代码随想录
状态:AC


Java代码:

import java.util.*;class Main {public static int[][] dir = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};public static void dfs(int[][] arr, int x, int y) {arr[x][y] = 2;for (int i = 0; i < 4; i++) {int nextX = x + dir[i][0];int nextY = y + dir[i][1];if (nextX < 0 || nextY < 0 || nextX >= arr.length || nextY >= arr[0].length) {continue;}if (arr[nextX][nextY] == 1) {dfs(arr, nextX, nextY);}}}public static void main(String[] args) {Scanner scan = new Scanner(System.in);int n = scan.nextInt();int m = scan.nextInt();int[][] arr = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {arr[i][j] = scan.nextInt();}}scan.close();for (int i = 0; i < n; i++) {if (arr[i][0] == 1) {dfs(arr, i, 0);}if (arr[i][m - 1] == 1) {dfs(arr, i, m - 1);}}for (int i = 0; i < m; i++) {if (arr[0][i] == 1) {dfs(arr, 0, i);}if (arr[n - 1][i] == 1) {dfs(arr, n - 1, i);}}for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (arr[i][j] == 1) {arr[i][j] = 0;} else if (arr[i][j] == 2) {arr[i][j] = 1;}System.out.print(arr[i][j]);if (j == m - 1) {System.out.print("\n");} else {System.out.print(" ");}}}}
}

103. 水流问题

题目链接:KamaCoder
文档讲解:代码随想录
状态:AC


Java代码:

import java.util.*;class Main {public static int[][] dir = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};public static void dfs(int[][] arr, boolean[][] visited, int x, int y, int height) {// 如果已访问,直接返回if (visited[x][y]) {return;}// 标记当前节点为已访问visited[x][y] = true;// 向四个方向深度优先搜索for (int i = 0; i < 4; i++) {int nextX = x + dir[i][0];int nextY = y + dir[i][1];// 只访问高度 >= 当前高度的位置,保证水可以继续流动if (nextX >= 0 && nextY >= 0 && nextX < arr.length && nextY < arr[0].length&& arr[nextX][nextY] >= height) {dfs(arr, visited, nextX, nextY, arr[nextX][nextY]);}}}public static void main(String[] args) {Scanner scan = new Scanner(System.in);int n = scan.nextInt();int m = scan.nextInt();int[][] arr = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {arr[i][j] = scan.nextInt();}}scan.close();boolean[][] first = new boolean[n][m];boolean[][] second = new boolean[n][m];for (int i = 0; i < n; i++) {dfs(arr, first, i, 0, arr[i][0]);dfs(arr, second, i, m - 1, arr[i][m - 1]);}for (int i = 0; i < m; i++) {dfs(arr, first, 0, i, arr[0][i]);dfs(arr, second, n - 1, i, arr[n - 1][i]);}for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (first[i][j] && second[i][j]) {System.out.println(i + " " + j);}}}}
}

104.建造最大岛屿

题目链接:KamaCoder
文档讲解:代码随想录
状态:AC


Java代码:

import java.util.*;class Main {public static int count = 0;public static int[][] dir = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};public static void dfs(int[][] arr, boolean[][] visited, int x, int y, int mark) {for (int i = 0; i < 4; i++) {int nextX = x + dir[i][0];int nextY = y + dir[i][1];if (nextX < 0 || nextY < 0 || nextX >= arr.length || nextY >= arr[0].length || visited[nextX][nextY]) {continue;}if (arr[nextX][nextY] == 1) {count++;arr[nextX][nextY] = mark;visited[nextX][nextY] = true;dfs(arr, visited, nextX, nextY, mark);}}}public static void main(String[] args) {Scanner scan = new Scanner(System.in);int n = scan.nextInt();int m = scan.nextInt();int[][] arr = new int[n][m];boolean[][] visited = new boolean[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {arr[i][j] = scan.nextInt();}}scan.close();Map<Integer, Integer> map = new HashMap<>();int mark = 2;boolean isAll = true;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (arr[i][j] == 0) {isAll = false;}if (arr[i][j] == 1 && !visited[i][j]) {arr[i][j] = mark;visited[i][j] = true;count++;dfs(arr, visited, i, j, mark);map.put(mark, count);count = 0;mark++;}}}if (isAll) {System.out.println(m * n);return;}int value = 0;int res = 0;Set<Integer> set = new HashSet<>();for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (arr[i][j] == 0) {set.clear();value = 0;for (int z = 0; z < 4; z++) {int nextX = i + dir[z][0];int nextY = j + dir[z][1];if (nextX < 0 || nextY < 0 || nextX >= arr.length || nextY >= arr[0].length) {continue;}if (map.containsKey(arr[nextX][nextY]) && !set.contains(arr[nextX][nextY])) {set.add(arr[nextX][nextY]);value += map.get(arr[nextX][nextY]);}}res = Math.max(res, value);}}}System.out.println(res + 1);}
}
http://www.lryc.cn/news/547846.html

相关文章:

  • llamafactory大模型微调教程(周易大模型案例)
  • excel 斜向拆分单元格
  • 【JAVA架构师成长之路】【JVM实战】第2集:生产环境内存飙高排查实战
  • MATLAB实现遗传算法优化风电_光伏_光热_储热优化
  • JCRQ1河马算法+四模型对比!HO-CNN-GRU-Attention系列四模型多变量时序预测
  • react中的fiber和初次渲染
  • LLM 大模型基础认知篇
  • leetcode700-二叉搜索树中的搜索
  • 《MySQL三大核心日志解析:Undo Log/Redo Log/Bin Log对比与实践指南》
  • java中实体类常见的设计模式
  • 【够用就好006】如何从零开发游戏上架steam面向AI编程的godot独立游戏制作实录001流程
  • 发行思考:全球热销榜的频繁变动
  • docker目录挂载与卷映射的区别
  • `label` 标签的 `for` 属性详解
  • 公开笔记:自然语言处理(NLP)中文文本预处理主流方法
  • 【一个月备战蓝桥算法】递归与递推
  • 算法策略深度解析与实战应用
  • 【LeetCode 热题 100】3. 无重复字符的最长子串 | python 【中等】
  • 计算机网络(1) 网络通信基础,协议介绍,通信框架
  • 在 Docker 中,无法直接将外部多个端口映射到容器内部的同一个端口
  • 计算机网络开发(2)TCP\UDP区别、TCP通信框架、服务端客户端通信实例
  • ubuntu打包 qt 程序,不用每次都用linuxdeployqt打包
  • 【Python项目】基于深度学习的车辆特征分析系统
  • C++(初阶)(二)——类和对象
  • JS—组成:2分钟掌握什么是ECMAScript操作,什么是DOM操作,什么是BOM操作
  • ArcGIS操作:10 投影坐标系转地理坐标系
  • NVIDIA Jetson Nano的国产替代,基于算能BM1684X+FPGA+AI算力盒子,支持deepseek边缘部署
  • c++全排列
  • VSCode 配置优化指南:打造极致高效的前端开发环境
  • 利用 ArcGIS Pro 快速统计省域各市道路长度的实操指南