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

代码随想录第51天|岛屿数量(深搜)、岛屿数量(广搜)、岛屿的最大面积

1.岛屿数量(深搜) ---》模板题

版本一写法:下一个节点是否能合法已经判断完了,传进dfs函数的就是合法节点。

#include <iostream>
#include <vector>
using namespace std;int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void dfs(const vector<vector<int> > &grid, vector<vector<bool> > &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 || nextx >= grid.size() || nexty < 0 ||nexty >= grid[0].size())continue; // 越界了,直接跳过if (!visited[nextx][nexty] &&grid[nextx][nexty] == 1){ // 没有访问过的 同时 是陆地的visited[nextx][nexty] = true;dfs(grid, visited, nextx, nexty);}}
}int main() {int n, m;cin >> n >> m;vector<vector<int> > grid(n, vector<int>(m, 0));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {cin >> grid[i][j];}}vector<vector<bool> > visited(n, vector<bool>(m, false));int result = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (!visited[i][j] && grid[i][j] == 1) {visited[i][j] = true;result++;                 // 遇到没访问过的陆地,+1dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true}}}cout << result << endl;
}

版本二:不管节点是否合法,上来就dfs,然后在终止条件的地方进行判断,不合法再retur

void dfs(const vector<vector<int>> &grid, vector<vector<bool>> &visited, int x, int y)
{if(visited[x][y]||grid[x][y]==0)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 || nextx >= grid.size() || nexty < 0 ||nexty >= grid[0].size())continue; // 越界了,直接跳过dfs(grid, visited, nextx, nexty);}
}int main()
{...vector<vector<bool>> visited(n, vector<bool>(m, false));int result = 0;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (!visited[i][j] && grid[i][j] == 1){result++;                 // 遇到没访问过的陆地,+1dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true}}}cout << result << endl;
}

但是版本一比版本二要高效,避免了无用的递归。

2.岛屿数量(广搜)

只要 加入队列就代表 走过,就需要标记,而不是从队列拿出来的时候再去标记走过.

#include <iostream>
#include <vector>
#include <queue>
using namespace std;int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void bfs(const vector<vector<int>> &grid, vector<vector<bool>> &visited, int x, int y)
{queue<pair<int, int>> que;que.push({x, y});visited[x][y] = true;while(!que.empty()){pair<int,int>cur = que.front();que.pop();int curx = cur.first;int cury = cur.second;for(int i = 0; i < 4; i++){int nextx = curx + dir[i][0];int nexty = cury + dir[i][1];if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;if(!visited[nextx][nexty]&&grid[nextx][nexty]==1){que.push({nextx, nexty});visited[nextx][nexty] = true;}}}
}int main()
{int n, m;cin >> n >> m;vector<vector<int>> grid(n, vector<int>(m, 0));for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){cin >> grid[i][j];}}vector<vector<bool>> visited(n, vector<bool>(m, false));int result = 0;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (!visited[i][j] && grid[i][j] == 1){result++;                 // 遇到没访问过的陆地,+1bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true}}}cout << result << endl;
}

3.岛屿的最大面积

#include <iostream>
#include <vector>
using namespace std;int count = 0;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
void dfs(vector<vector<int>> &grid, vector<vector<bool>> &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 >= grid.size() ||nexty >= grid[0].size()) {continue;}if(!visited[nextx][nexty]&&grid[nextx][nexty]==1){visited[nextx][nexty] = true;count++;dfs(grid,visited,nextx,nexty);}}
}int main(void) {int n, m;cin >> n >> m;vector<vector<int>> grid(n, vector<int>(m, 0));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {cin >> grid[i][j];}}vector<vector<bool>> visited(n, vector<bool>(m, false));int result = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (!visited[i][j] && grid[i][j] == 1) {visited[i][j] = true;count=1;//重置dfs(grid, visited, i, j);result = max(result, count);}}}cout<<result<<endl;return 0;
}

http://www.xdnf.cn/news/481231.html

相关文章:

  • 2025年Ai写PPT工具推荐,这5款Ai工具可以一键生成专业PPT
  • Java—— 方法引用 : :
  • 【第76例】IPD流程实战:华为业务流程架构BPA进化的4个阶段
  • ROS2学习(3)------架构概述
  • 【数据仓库面试题合集①】数据建模高频面试题及解析
  • 平衡智慧在日常生活中的落地实践:构建和谐生活的行动指南
  • MYSQL创建索引的原则
  • 单例模式深度解析:从原理到高阶应用实践
  • 麒麟桌面系统文件保险箱快捷访问指南:让重要文件夹一键直达桌面!
  • [MySQL实战] 主从复制(Replication)搭建教程:实现读写分离与高可用基础
  • 项目QT+ffmpeg+rtsp(一)——Qt的安装和rtsp的测试
  • python的家教课程管理系统
  • spring cloud gateway 源码解析
  • 嵌入式单片机中STM32F1演示寄存器控制方法
  • Linux系统编程——exec族函数
  • 【生成式AI文本生成实战】DeepSeek系列应用深度解析
  • Crowdfund Insider聚焦:CertiK联创顾荣辉解析Web3.0创新与安全平衡之术
  • day22-数据结构之 栈队列
  • git版本控制学习
  • AB Download Manager v1.5.8 开源免费下载工具
  • AI 编程 “幻觉” 风险频发?飞算 JavaAI 硬核技术筑牢安全防线
  • 1688代采系统商品采集下单支付解决方案|官方API接口接入指南
  • Android从单体架构迁移到模块化架构。你会如何设计模块划分策略?如何处理模块间的通信和依赖关系
  • 开源轻量级地图解决方案leaflet
  • Mac安装Navicat16
  • mac的Cli为什么输入python3才有用python --version显示无效,pyenv入门笔记,如何查看mac自带的标准库模块
  • 前端面经 手写Promise
  • GTS-400 系列运动控制器板卡介绍(三十五)---读取运动控制器版本号
  • 大语言模型 09 - 从0开始训练GPT 0.25B参数量 补充知识之数据集 Pretrain SFT RLHF
  • 车道线检测----CLRKDNet