算法训练营|图论第二天 99.岛屿数量 100.岛屿的最大面积

news/2024/9/15 11:46:06/ 标签: 算法, 图论

题目:99.岛屿数量

题目链接:

99. 岛屿数量 (kamacoder.com)

代码:

深度优先搜索:

#include<bits/stdc++.h>
using namespace std;
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 (grid[nextx][nexty] == 1 && !visited[nextx][nexty]) {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];}}int result = 0;vector<vector<bool>>visited(n, vector<bool>(m, false));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (grid[i][j] == 1 && visited[i][j] == false) {dfs(grid, visited, i, j);result++;}}}cout << result << endl;
}

广度优先搜索:

#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {queue<pair<int, int>>que;que.push({ x,y });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 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size()) continue;if (grid[nextx][nexty] && !visited[nextx][nexty]) {que.push({ nextx,nexty });visited[nextx][nexty] = true;bfs(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];}}int result = 0;vector<vector<bool>>visited(n, vector<bool>(m, false));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (grid[i][j] == 1 && !visited[i][j]) {result++;bfs(grid, visited, i, j);}}}cout << result << endl;
}

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

题目链接:

100. 岛屿的最大面积 (kamacoder.com)

代码:

广搜:
 

#include<bits/stdc++.h>
using namespace std;
int ans;
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {queue<pair<int, int>>que;que.push({ x,y });ans++;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 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size()) continue;if (grid[nextx][nexty] == 1 && !visited[nextx][nexty]) {visited[nextx][nexty] = true;que.push({ nextx,nexty });ans++;}}}
}
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];}}int result = 0;vector<vector<bool>>visited(n, vector<bool>(m, false));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (!visited[i][j] && grid[i][j]) {ans = 0;bfs(grid, visited, i, j);result = max(result, ans);}}}cout << result << endl;
}

 深搜:

#include<bits/stdc++.h>
using namespace std;
int ans;
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) {if (grid[x][y] == 0 || visited[x][y]) return;visited[x][y] = true;ans++;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;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];}}int result = 0;vector<vector<bool>>visited(n, vector<bool>(m, false));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (!visited[i][j] && grid[i][j]) {ans = 0;dfs(grid, visited, i, j);result = max(result, ans);}}}cout << result << endl;
}

 深搜思路2:

#include<bits/stdc++.h>
using namespace std;
int ans;
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]) {visited[nextx][nexty] = true;ans++;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];}}int result = 0;vector<vector<bool>>visited(n, vector<bool>(m, false));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (!visited[i][j] && grid[i][j]) {ans = 0;dfs(grid, visited, i, j);result = max(result, ans);}}}cout << result << endl;
}


http://www.ppmy.cn/news/1519397.html

相关文章

MySQL唯一索引大小写敏感性问题及字符集深入解析

1. 问题背景与描述 在实际生产环境中&#xff0c;我们遇到了一个插入重复异常的问题。具体表现在长链接转换为短链接的过程中&#xff0c;生成的短链被插入数据库时触发了唯一索引的冲突错误。错误的根本原因在于数据库使用了不区分大小写的排序规则&#xff0c;导致两个看似不…

Linux下安装MySQL8.0

一、安装 1.下载安装包 先创建一个mysql目录&#xff0c;在将压缩包下载到此 # 下载tar包 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz等待下载成功 2.解压mysql8.0安装包 tar xvJf mysql-8.0.20-linux-glibc2.12-x86…

尝试用java spring boot+VUE3实现前后端分离部署

前言 这几天开学了&#xff0c;公司这边几个和学校对接的项目都挺忙的&#xff0c;然后我又开始有点闲的情况了。问大佬能不能继续看看若依的项目&#xff0c;大佬让我自己去学了。在看若依的项目的时候在想&#xff0c;python的FLASK后端实现和JAVA spring boot的实现差别大不…

【GPT】Coze使用开放平台接口-【2】创建工作流-语音伪造检测工作流

在Coze使用开放平台接口-【1】创建插件&#xff0c;我们已经成功创建了开放平台的插件&#xff0c;也创建了对应的工具。本文档就根据创建好的插件&#xff0c;来创建对应的工作流&#xff0c;来让接口能够用起来。 下面直接用现成的插件快商通AI开放平台&#xff0c;来创建语音…

深度学习实战1--决策树与随机森林(最新版本不报错)

1.乳腺癌数据集简介 乳腺癌数据集包含了美国威斯康星州记录的569个病人的乳腺癌的病情&#xff0c;包含30个维度的生理指标数据(特征),以及乳腺癌是恶性还是良性的标签。因为这是一个二分类问题&#xff0c; 也叫二类判别数据集。 2.实战任务 这数据主要包含569个样本。每个样…

【3.6】贪心算法-解救生艇问题

一、题目 第 i 个人的体重为 people[i]&#xff0c;每艘船可以承载的最大重量为 limit。 每艘船最多可同时载两人&#xff0c;但条件是这些人的重量之和最多为 limit 。 返回载到每一个人所需的最小船数。(保证每个人都能被船载)。 二、解题思路 题目要求每艘船最多能载两人&…

安美数字酒店宽带运营系统-任意文件读取

漏洞描述&#xff1a; 安美数字酒店宽带运营系统 weather.php 接口存在任意文件读取漏洞&#xff0c;未经身份验证攻击者可通过该漏洞读取系统重要文件&#xff08;如数据库配置文件、系统配置文件&#xff09;、数据库配置文件等等&#xff0c;导致网站处于极度不安全状态 fo…

c++中的匿名对象及内存管理及模版初阶

目录 c中的匿名对象 日期到天数的转换 深入理解析构 深入理解拷贝构造 内存管理 全局变量和static变量的区别&#xff1b; malloc/calloc/realloc的区别 new和delete的意义&#xff1f; operator new与operator delete函数 对比malloc和new operator 定制operator ne…

OceanBase 功能解析之 Binlog Service

前言 MySQL&#xff0c;是在全球广泛应用的开源关系型数据库&#xff0c;除了其稳定性、可靠性和易用性&#xff0c;他早期推出的二进制日志功能&#xff0c;即binlog&#xff0c;也是MySQL广受欢迎的原因。 MySQL binlog&#xff0c;即二进制日志&#xff0c;是 MySQL 中用于…

django之ForeignKey、OneToOneField 和 ManyToManyField

在Django中&#xff0c;ForeignKey、OneToOneField 和 ManyToManyField 是用于定义模型之间关系的字段类型。 ForeignKey ForeignKey 用于定义多对一的关系。例如&#xff0c;一个Employee可以属于一个Department&#xff0c;一个Department可以有多个Employee。 from djang…

String的基本特;String的内存分配;字符串拼接操作;intern()的使用;经典面试题

目录 String的基本特性String的内存分配字符串拼接操作intern()的使用经典面试题 String的基本特性 创建的两种方式 String s “a” //字面量的定义方式 String s2 new String(“fd”) String类声明为final&#xff0c;不可被继承&#xff0c;实现了Serializable接口&#xf…

昇腾AI处理器的计算核心 - AI Core即DaVinci Core

昇腾AI处理器的计算核心 - AI Core即DaVinci Core flyfish 从一段代码的解释开始 template <typename T> class GlobalTensor { public:void setGlobalBuffer(T* buffer, uint32_t buffersize) {// 在这里实现设置全局缓冲区的逻辑} };语法的说明&#xff0c;主要用于…

优化 Webpack 打包体积的思路

在现代前端开发中&#xff0c;优化 Webpack 打包体积是提升应用性能的重要手段。以下是一些有效的优化思路&#xff1a; 提取第三方库&#xff1a;将第三方库单独打包&#xff0c;并通过 CDN 引入。这样不仅减少了打包体积&#xff0c;还利用了 CDN 的缓存优势&#xff0c;提高…

索迪迈科技油罐车监控系统中车载摄像头的布局策略

随着科技的不断发展&#xff0c;车载监控系统在油罐车上的安装已经成为了一种趋势。这不仅大大降低了车辆的安全隐患与运营成本&#xff0c;更对石油运输企业优化资源配置、提高市场竞争力起到了积极的促进作用。那么&#xff0c;在油罐车监控系统中&#xff0c;如何合理布局车…

html table tbody deleteRow有残留?

html table tbody deleteRow有残留? 问题描述&#xff1a;这个问题描述的是在使用 HTML 的 deleteRow 方法从一个 table 的 tbody 中删除行时&#xff0c;表格中仍然存在某些行。 参考方法1&#xff1a;表格移除多行的时候, 移除行数字索引顺序要从大到小, 而不能从小到大。 …

【华为OD】2024D卷——查找众数与中位数

题目&#xff1a; 众数是指一组数据中出现次数量多的那个数&#xff0c;众数可以是多个。 中位数是指把一组数据从小到大排列&#xff0c;最中间的那个数&#xff0c;如果这组数据的个数是奇数&#xff0c;那最中间那个就是中位数&#xff0c;如果这组数据的个数为偶数&#xf…

【我的Android进阶之旅】使用TabLayout自定义一个TitleTabView

文章目录 零、效果图一、自定义一个TitleTabView1.1 自定义属性(attrs.xml 中)1.2 自定义TitleTabView1.3 TabItem的子布局1.4 颜色值二、在 XML 中使用 `TitleTabView`2.1 布局文件(XML)2.1.1属性说明三、在 Kotlin 中使用 `TitleTabView`:零、效果图 其中Tab 2是选中的效果…

【笔记】数据结构——8月27日

toc 静态链表 静态链表的存储结构 #define maxn 15struct space {int cur;int key; }s[maxn];int LocateElem_SL(SLinkList *s,ElemType e) {//在静态链表中查找第一个值为e的元素//若找到&#xff0c;则返回它在链表中的位置&#xff0c;否则返回0 is[0].cur;while(i&…

使用本地IP无法访问前端项目的问题

说明&#xff1a;记录一次使用本机IP无法访问前端项目的问题 场景&解决 前端项目在我本机电脑上部署完成&#xff0c;我想通过局域网的IP把地址发给测试&#xff0c;发现使用localhost和127.0.0.0都能访问到前端项目&#xff0c;但是这个地址只能在自己的电脑上访问。 解…

记一次学习--webshell绕过(利用清洗函数)

目录 样本 样本修改 样本 <?php $a array("t", "system"); shuffle($a); $a[0]($_POST[1]); 通过 shuffle 函数打乱数组,然后通过$a[0]取出第一个元素&#xff0c;打乱后第一个元素可能是t也可能是system。然后再进行POST传参进行命令执行。 这里抓…