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

代码随想录第51天

99.岛屿数量 深搜

import java.util.*;class Main{static int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};static boolean[][] visited;public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int[][] grids = new int[n][m];for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {grids[i][j] = sc.nextInt();}}int cnt = 0;visited = new boolean[n][m];for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {if(grids[i][j] == 1 && !visited[i][j]) {cnt++;dfs(grids, i, j);}}}System.out.println(cnt);}private static void dfs(int[][] grids, int x, int y) {visited[x][y] = true;for(int i = 0; i < 4; i++) {int nextX = x + directions[i][0];int nextY = y + directions[i][1];if(check(grids, nextX, nextY)) {dfs(grids, nextX, nextY);}}}private static boolean check(int[][] grids, int x, int y) {int n = grids.length;int m = grids[0].length;return x >= 0 && x < n && y >= 0 && y < m && !visited[x][y] && grids[x][y] == 1;}
}

99.岛屿数量 广搜

import java.util.*;// dfs
class Main {public static void main (String[] args) {Main main = new Main();Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int[][] island = new int[n][m];for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {island[i][j] = sc.nextInt();}}int result = main.getIslandNum(island);System.out.println(result);}public int getIslandNum(int[][] island) {int numsIsland = 0;int row = island.length;int col = island[0].length;for (int i = 0; i < row; ++i) {for (int j = 0; j < col; ++j) {if (island[i][j] == 1) {++numsIsland;dfs(island, i, j);}}}return numsIsland;}public void dfs(int[][] island, int x, int y) {int row = island.length;int col = island[0].length;if (x < 0 || y < 0 || x >= row || y >= col || island[x][y] == 0) {return;}island[x][y] = 0; // 标记为已经访问dfs(island, x, y + 1);dfs(island, x, y - 1);dfs(island, x + 1, y);dfs(island, x - 1, y);}
}

100.岛屿的最大面积

#include <stdio.h>
#include <stdlib.h>// 四方向向量 下 右 上 左(逆时针)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};int bfs(int **G, int N, int M, int x, int y);int main(void) {int N, M;scanf("%d %d", &N, &M);// 创建N行M列矩阵int **G = (int **)calloc(N, sizeof(int *));for(int i = 0; i < N; ++ i) {G[i] = (int *)calloc(M, sizeof(int));for(int j = 0; j < M; ++ j) {scanf("%d", &G[i][j]);}}// DFS函数int max = 0;int curMax;for(int i = 0; i < N; ++ i) {for(int j = 0; j < M; ++ j) {if (G[i][j]) {G[i][j] = 0;curMax = bfs(G, N, M, i, j);max = max < curMax ? curMax : max;}}}printf("%d\n", max);// 释放内存for(int i = 0; i < N; ++ i) free(G[i]);free(G);return 0;
}int bfs(int **G, int N, int M, int x, int y) {int queue[500][2];int front = 0, rear = 0;queue[rear][0] = x;queue[rear ++][1] = y;int curx, cury;int res = 1;while(front < rear) {curx = queue[front][0];cury = queue[front ++][1];for(int i = 0; i < 4; ++ i) {int nextx = curx + dx[i];int nexty = cury + dy[i];if (nextx >= 0 && nextx < N && nexty >= 0 && nexty < M) {if (G[nextx][nexty]) {G[nextx][nexty] = 0;++ res;queue[rear][0] = nextx;queue[rear ++][1] = nexty;}}}}return res;
}
http://www.lryc.cn/news/506476.html

相关文章:

  • 基础库httpx的使用
  • c++中如何保持结构体的线程安全?3D坐标的线程安全:从理论到最优解
  • Zabbix6.0升级为6.4
  • 答题考试系统v1.6.1高级版源码分享+uniapp+搭建测试环境
  • 【Lua热更新】下篇 -- 更新中
  • 射频测试入门学习(三)——程控仪器是怎样和电脑连接通信的
  • 并发控制之Semaphore
  • 第R3周:RNN-心脏病预测
  • 【数值特性库】入口文件
  • RestTemplate实时接收Chunked编码传输的HTTP Response
  • GIT区域介绍及码云+GIt配置仓库
  • 网络安全怎么学习
  • PugiXML,一个高效且简单的 C++ XML 解析库!
  • Linux设备树的驱动开发
  • 连锁?下沉?AI?2025年餐饮新活力!
  • Javascript中如何实现函数缓存?函数缓存有哪些应用场景?
  • 子页面访问父页面
  • 芯片级IO (Pad) Ring IP Checklist
  • 计算机毕业设计论文指导
  • Electron-Vue 开发下 dev/prod/webpack server各种路径设置汇总
  • Vue.js前端框架教程9:Vue插槽slot用法
  • 初学stm32 --- NVIC中断
  • Jest 入门指南:从零开始编写 JavaScript 单元测试
  • 【Java Web】Axios实现前后端数据异步交互
  • React 第十七节 useMemo用法详解
  • 鸿蒙项目云捐助第十五讲云数据库的初步使用
  • 如何构建一个可信的联邦RAG系统。
  • 【深度学习之三】FPN与PAN网络详解
  • Qt学习笔记第71到80讲
  • 以管理员身份运行