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

代码随想录算法训练营第五六天| 99. 岛屿数量 100. 岛屿的最大面积


今日任务

99. 岛屿数量 深度搜搜
99. 岛屿数量 广度搜索
100. 岛屿的最大面积

99. 岛屿数量

题目链接: 99. 岛屿数量

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 (nextX < 0 || nextY < 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 scanner = new Scanner(System.in);int m = scanner.nextInt();int n = scanner.nextInt();int[][] grid = new int[m][n];for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {grid[i][j] = scanner.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);}}

99. 岛屿数量


题目链接: 99. 岛屿数量

import java.util.Scanner;
import java.util.*;public class Main {public static int[][] dir = {{0, 1},{1, 0},{-1, 0},{0, -1}};public static void bfs(boolean[][] visited, int x, int y, int[][] grid){Queue<int[]>  queue = new LinkedList<>();queue.add(new int[]{x, y});visited[x][y] = true;while (!queue.isEmpty()) {int curX = queue.peek()[0];int curY = queue.peek()[1];queue.poll();for (int i = 0; i < 4; i++) {int nextX = curX + dir[i][0];int nextY = curY + dir[i][1];if (nextX < 0 || nextY < 0 || nextX >= grid.length || 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 scanner = new Scanner(System.in);int m = scanner.nextInt();int n = scanner.nextInt();int[][] grid = new int[m][n];for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {grid[i][j] = scanner.nextInt();}}int res = 0;boolean[][] visited = new boolean[m][n];for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (!visited[i][j] && grid[i][j] == 1) {res++;bfs(visited, i, j, grid);}}}System.out.println(res);}}


100. 岛屿的最大面积

题目链接: 100. 岛屿的最大面积

import java.util.*;
public class Main {public static int[][] dir = {{0, 1},{1, 0},{-1, 0},{0, -1}};public static int res = 0;public static void bfs(boolean[][] visited, int x, int y, int[][] grid){Queue<int[]> queue = new LinkedList<>();queue.add(new int[]{x, y});visited[x][y] = true;while (!queue.isEmpty()) {int curX = queue.peek()[0];int curY = queue.peek()[1];queue.poll();for (int i = 0; i < 4; i++) {int nextX = curX + dir[i][0];int nextY = curY + dir[i][1];if (nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) {continue;}if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {res++;queue.add(new int[]{nextX, nextY});visited[nextX][nextY] = true;}}}}public static void main(String[] args){Scanner scanner = new Scanner(System.in);int m = scanner.nextInt();int n = scanner.nextInt();int[][] grid = new int[m][n];for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {grid[i][j] = scanner.nextInt();}}int max = 0;boolean[][] visited = new boolean[grid.length][grid[0].length];for (int i = 0; i < grid.length; i++) {for (int j = 0; j < grid[0].length; j++) {if (!visited[i][j] && grid[i][j] == 1) {res = 1;visited[i][j] = true;bfs(visited, i, j, grid);}max = Math.max(max, res);}}System.out.println(max);}    }


http://www.mrgr.cn/news/46740.html

相关文章:

  • JVM 触发类加载的条件有哪些?
  • 认识机器学习中的经验风险最小化准则
  • SpringData-Redis缓存之RedisTemplate
  • 【C语言 结构体联合体】字节对齐与字节大小计算
  • 【计算机网络】深入浅出计算机网络
  • “片段视频下载与合并工具“
  • 搜维尔科技:Haption远程操作项目模拟项目
  • 动态内存分配 VS 静态内存分配
  • 【虚拟化】内核级虚拟化技术KVM介绍,全/半虚拟化的区别,使用libvirt搭建虚拟化平台(go/java/c++)
  • 这个问题做项目的时给某些客户普及过,这里再给你普及一下
  • FireFox简单设置设置
  • 创建osd加入集群
  • linux基础-----基础命令+较新替代命令汇总详解
  • CF E. Money Buys Happiness
  • 【动态规划】
  • Http 协议和 RPC 协议有什么区别?
  • JavaEE: HTTPS的魅力与优势揭秘
  • 1. Keepalived概念和作用
  • 代码随想录算法训练营第二十九天|93.复原IP地址 78.子集 90.子集II
  • PostgreSQL的学习心得和知识总结(一百五十三)|[performance]将 OR 子句转换为 ANY 表达式
  • 苹果电脑磁盘满了怎么清理内存?必看清理秘籍
  • maven加载依赖成功但是引入import不了包,注解报错
  • ★ 算法OJ题 ★ 二分查找算法
  • 网络安全产品类型
  • 数据结构——排序(交换排序)
  • 路由协议常见知识点