代码随想录训练营Day 65|卡码网99岛屿数量 深搜、99.岛屿数量 广搜 、100.岛屿的最大面积

news2024/10/6 12:20:44

1.岛屿数量 深搜

99. 岛屿数量 | 代码随想录

 代码:(在符合递归条件时进行递归处理)

#include <iostream>
#include <vector>
using namespace std;
int dir[4][2] = {1,0,0,1,-1,0,0,-1}; // 表示4个方向 上下左右
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++;
                dfs(grid,visited,i,j);
            }
        }
    }
    // 输出
    cout << result << endl;
}

代码:(在递归中用终止条件和处理逻辑)

#include <iostream>
#include <vector>
using namespace std;
int dir[4][2] = {1,0,0,1,-1,0,0,-1}; // 表示4个方向 上下左右
void dfs(const vector<vector<int>> &grid,vector<vector<bool>>&visited,int x,int y){
    // 终止条件
    if(visited[x][y] == true || 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(){
    // 输入
    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++;
                dfs(grid,visited,i,j);
            }
        }
    }
    // 输出
    cout << result << endl;
}

 思路:

这道题的思路就是用visited数组来进行标记我们访问过的岛屿,每发现一个没有被标记过的岛屿就进行对岛屿个数的统计,并对其进行访问后的标记。

在代码1中,是在符合深搜条件的情况下进行深搜的调用,因为在调用前就已经判断了,所以把visited的标记直接在调用前就进行处理了。

在代码2中,是由深搜的终止条件来判断是不是要进行下面的代码。在深搜的开始就是判断是不是到了终止条件,如果没有,就可以进行visited的标记访问了。

2.岛屿深度 广搜

99. 岛屿数量 | 代码随想录

代码:

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {1,0,0,1,-1,0,0,-1}; // 表示4个方向 上下左右
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++;
                bfs(grid,visited,i,j);
            }
        }
    }
    // 输出
    cout << result << endl;
}

思路:

要注意,在元素加入对列时就进行标记,不然就会进行重复操作,导致超时。 

        深搜是进行递归操作。

        广搜是利用一个数据结构来保存我们遍历过的结点,一般用的是对列。

        将元素的下标存入队列中,然后标记为已访问。

        然后就是从这个结点出发,去把它四周的岛屿结点都标记为已访问(因为这些都只算作一个岛屿) ,我们用的是for循环,将此节点出栈,遍历它四个方向的相邻结点,遇到满足要求的(未访问且是岛屿结点),就将其入栈,并且立刻标记为已经访问。

3.岛屿的最大面积

 100. 岛屿的最大面积 | 代码随想录

代码:dfs(满足深搜条件时,再进行深搜)

#include <iostream>
#include <vector>
using namespace std;
int count;
int dir[4][2] = {1,0,0,1,-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;
            count++;
            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] == 1){
                count = 1;
                visited[i][j]=true;
                dfs(grid,visited,i,j);
                result = max(count,result);
            }
        }
    }
    // 输出
    cout << result << endl;
}

 

代码:dfs(用终止条件) 

#include <iostream>
#include <vector>
using namespace std;
int count;
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
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;
    count++;
    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(){
    // 输入
    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++){
                count = 0;
                dfs(grid,visited,i,j);
                result = max(count,result);
        }
    }
    // 输出
    cout << result << endl;
}

代码: bfs

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int count;
int dir[4][2] = {1,0,0,1,-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;
    count++;
    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;
                count++;
            }
        }
    }
}
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] == 1){
                count = 0;
                bfs(grid,visited,i,j);
                result = max(count,result);
            }
        }
    }
    // 输出
    cout << result << endl;
}

思路:这道题就是在进行visited标记的适合,下一步就对本次循环的访问结点进行计数,count++。这里巧妙地在main函数里的第二次for循环里将count先置为0。然后调用深搜或广搜的适合,从0开始技术,就可以达到求出来的是一个岛屿的面积。用result来记录这些岛屿面积的最大值,然后输出即可。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1847263.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

每日练题(py,c,cpp).6_19,6_20

检验素数 from math import sqrt a int(input("请输入一个数&#xff1a;")) for i in range(2,int(sqrt(a))):if a%i 0:print("该数不是素数")breakelse: print("该数是素数")# # 1既不是素数也不是合数 # #可以用flag做标志位 # b int(…

向“黑公关”开战,比亚迪悬赏500万征集恶意诋毁线索

近日&#xff0c;比亚迪品牌及公关处总经理李云飞在微博发文&#xff0c;面向社会公开征集黑公关证据。 微博中&#xff0c;李云飞写道&#xff1a;“近期&#xff0c;我们收到多方提醒&#xff1a;某车企在使用黑公关手段&#xff0c;对我司品牌及产品进行贬低、拉踩和恶意诋…

c++里对 new 、delete 运算符的重载

&#xff08;1&#xff09;c 里 我们可以用默认的 new 和 delete 来分配对象和回收对象。 new 可以先申请内存&#xff0c;再调用对象的构造函数&#xff1b; delete 则先调用对象的析构函数&#xff0c;再回收内存。当然&#xff0c;当我们为类定义了 operator new () 和 oper…

Qt坐标系统

目录 概述 渲染 逻辑表示 锯齿绘制 转换 模拟时钟示例 Window-Viewport转换 概述 坐标系统由QPainter类控制。与QPaintDevice和QPaintEngine类一起&#xff0c;QPainter构成了Qt绘画系统的基础。QPainter用于执行绘制操作&#xff0c;QPaintDevice是一个二维空间的抽象&…

L54--- 404.左叶子之和(深搜)---Java版

1.题目描述 2.思路 递归遍历左子树 &#xff0c;然后再把左子树的和相加 3.代码实现 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val …

JavaScript之类(1)

class基础语法结构&#xff1a; 代码&#xff1a; class MyClass {constructor() { ... }method1() { ... }method2() { ... }method3() { ... }... } 解释&#xff1a; 属性解释class是我们定义的类型(类)MyClass是我们定义的类的名称 constructor()我们可以在其中初始化对象m…

linux中Java程序调用C程序中方法的实现方式浅析

在Linux中&#xff0c;Java程序可以通过JNI&#xff08;Java Native Interface&#xff09;来调用C程序的方法。 Linux系统环境&#xff0c;Java调用C的主要流程如下&#xff1a; 1、创建Java类文件&#xff0c;如NativeLibrary.java 2、编写Java代码&#xff0c;加载.so共享库…

HTTP 状态码详解及使用场景

目录 1xx 信息性状态码2xx 成功状态码3xx 重定向状态码4xx 客户端错误状态码5xx 服务器错误状态码 HTTP思维导图连接&#xff1a;https://note.youdao.com/s/A7QHimm0 1xx 信息性状态码 100 Continue&#xff1a;表示客户端应继续发送请求的其余部分。 使用场景&#xff1a;客…

20240621在飞凌的OK3588-C开发板的Buildroot系统中集成i2ctool工具

20240621在飞凌的OK3588-C开发板中打开i2ctool工具 2024/6/21 17:44 默认继承的i2c工具&#xff1a; rootrk3588-buildroot:/# rootrk3588-buildroot:/# i2c i2c-stub-from-dump i2cdump i2cset i2cdetect i2cget i2ctransfer rootrk3588-…

机器学习第四十四周周报 SAMformer

文章目录 week44 SAMformer摘要Abstract1. 题目2. Abstract3. 网络架构3.1 问题提出3.2 微型示例3.3 SAMformer 4. 文献解读4.1 Introduction4.2 创新点4.3 实验过程 5. 结论6.代码复现小结参考文献 week44 SAMformer 摘要 本周阅读了题为SAMformer: Unlocking the Potential…

开启文物保护新篇章——智能RFID文物藏品库房管理系统

在历史的长河中&#xff0c;文物不仅是见证文明的瑰宝&#xff0c;更是文化传承的重要载体。这些承载着丰富历史和文化价值的珍贵文物&#xff0c;需要得到科学、精细的保护和管理。为了更好地守护和传承我们的文化遗产&#xff0c;我们荣幸地推出智能RFID文物藏品库房管理系统…

(2024,Vision-RWKV,线性复杂度双向注意力,四向标记移位)通过类似 RWKV 的架构实现高效且可扩展的视觉感知

Vision-RWKV: Efficient and Scalable Visual Perception with RWKV-Like Architectures 公和众与号&#xff1a;EDPJ&#xff08;进 Q 交流群&#xff1a;922230617 或加 VX&#xff1a;CV_EDPJ 进 V 交流群&#xff09; 目录 0. 摘要 2. 特征聚合机制 3. Vision-RWKV 3.…

怎么看电脑实时充电功率

因为我想测试不同的充电器给电脑充电的速度&#xff0c;所以就想找一款软件可以看电脑当前充电功率的软件&#xff0c;我给一个图 直接搜索就可以下载了&#xff0c;charge rate就是功率&#xff0c;这里是毫瓦&#xff0c;换算单位是 1000mw1w 所以我这里充电功率是65w&…

RocketMQ-记一次生产者发送消息存在超时异常

目录 1、背景说明 2、排查 2.1、防火墙 2.2、超时时间设置 2.3、服务器资源检查 2.3.1、内存、CPU等 2.3.2、磁盘空间 ​编辑 2.3.3、检查文件描述符 2.3.4、swap区 3、增加swap空间 3.1、创建目录 3.2、格式化 3.3、启动swap 3.4、查看效果 1、背景说明 在一次…

音视频开发—FFmpeg 打开摄像头进行RTMP推流

实验平台&#xff1a;Ubuntu20.04 摄像头&#xff1a;普通USB摄像头&#xff0c;输出格式为YUV422 1.配置RTMP服务器推流平台 使用Nginx 配置1935端口即可&#xff0c;贴上教程地址 ubuntu20.04搭建Nginxrtmp服务器) 2.配置FFmpeg开发环境 过程较为简单&#xff0c;这里不…

深度学习Week17——优化器对比实验

文章目录 深度学习Week17——优化器对比实验 一、前言 二、我的环境 三、前期工作 1、配置环境 2、导入数据 2.1 加载数据 2.2 检查数据 2.3 配置数据集 2.4 数据可视化 四、构建模型 五、训练模型 1、将其嵌入model中 2、在Dataset数据集中进行数据增强 六、模型评估 1、Accur…

IMU用于飞行坐姿校正

为了提升长途飞行的舒适度并预防乘客因不良坐姿导致的身体不适&#xff0c;来自荷兰上海两所大学的研究团队携手开发出一种创新的“舒适穿戴”设备&#xff0c;专为识别飞行中的坐姿设计。 研究团队制作了两种原型设备&#xff1a;一种追求极致舒适&#xff0c;另一种为紧身设…

增强-MIGO物料消耗需要将物料描述写到会计凭证的摘要里面

财务比较闲提的需求&#xff0c;有些物料消耗需要将物料描述写到会计凭证的摘要里面&#xff0c; 找了一下增强点&#xff0c;随便搞了一下&#xff0c;可以了。

20240620日志:TAS-MRAM的电阻开放分析

TAS-MRAM的电阻开放缺陷分析 1 MRAM介绍开放电阻的缺陷 1 MRAM介绍 MRAM(Magnetic random access memory)&#xff0c;磁随机存储器&#xff0c;利用磁性材料的状态来存储数据。MRAM的存储单元通常由一个磁隧道结&#xff08; M T J 茅台酒 MTJ^{茅台酒} MTJ茅台酒&#xff0c…

STM32小项目———感应垃圾桶

文章目录 前言一、超声波测距1.超声波简介2.超声波测距原理2.超声波测距步骤 二、舵机的控制三、硬件搭建及功能展示总结 前言 一个学习STM32的小白~ 有问题请评论区或私信指出 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、超声波测距 1.超声波…