代码随想录day52 101孤岛的总面积 102沉没孤岛 103水流问题 104建造最大岛屿

代码随想录day52 101孤岛的总面积 102沉没孤岛 103水流问题 104建造最大岛屿

101孤岛的总面积

代码随想录

#include <iostream>
#include <vector>using namespace std;
int count = 0;
int dir[4][2] = {{1, 0}, {0, 1}, {-1 ,0}, {0, -1}};void dfs(vector<vector<int>> &graph, int row, int col) {graph[row][col] = 0;count++;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1) {dfs(graph, nextRow, nextCol);}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {if (graph[i][0] == 1) {dfs(graph, i, 0);}if (graph[i][M - 1] == 1) {dfs(graph, i, M - 1);}}for (int j = 0; j < M; j++) {if (graph[0][j] == 1) {dfs(graph, 0, j);}if (graph[N - 1][j] == 1) {dfs(graph, N - 1, j);}}count = 0;for (int i = 1; i < N - 1; i++) {for (int j = 1; j < M - 1; j++) {if (graph[i][j] == 1) {dfs(graph, i, j);}}}cout << count;return 0;
}
#include <iostream>
#include <vector>
#include <queue>using namespace std;int count = 0;
int dir[4][2] = {{1, 0}, {0, 1}, {-1 ,0}, {0, -1}};void bfs(vector<vector<int>> &graph, int row, int col) {count++;graph[row][col] = 0;queue<pair<int, int>> que;que.push({row, col});while (!que.empty()){pair<int, int> pa = que.front();que.pop();for (int i = 0; i < 4; i++) {int nextRow = pa.first + dir[i][0];int nextCol = pa.second + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1) {count++;graph[nextRow][nextCol] = 0;que.push({nextRow, nextCol});}}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {if (graph[i][0] == 1) {bfs(graph, i, 0);}if (graph[i][M - 1] == 1) {bfs(graph, i, M - 1);}}for (int j = 0; j < M; j++) {if (graph[0][j] == 1) {bfs(graph, 0, j);}if (graph[N - 1][j] == 1) {bfs(graph, N - 1, j);}}count = 0;for (int i = 1; i < N - 1; i++) {for (int j = 1; j < M - 1; j++) {if (graph[i][j] == 1) {bfs(graph, i, j);}}}cout << count;return 0;
}

102沉没孤岛

代码随想录

#include <iostream>
#include <vector>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1 ,0}, {0, -1}};void dfs(vector<vector<int>> &graph, int row, int col) {graph[row][col] = 2;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1) {dfs(graph, nextRow, nextCol);}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {if (graph[i][0] == 1) {dfs(graph, i, 0);}if (graph[i][M - 1] == 1) {dfs(graph, i, M - 1);}}for (int j = 0; j < M; j++) {if (graph[0][j] == 1) {dfs(graph, 0, j);}if (graph[N - 1][j] == 1) {dfs(graph, N - 1, j);}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] != 0) {graph[i][j]--;}cout << graph[i][j] << " ";}cout << "\n";}return 0;
}
#include <iostream>
#include <vector>
#include <queue>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1 ,0}, {0, -1}};void bfs(vector<vector<int>> &graph, int row, int col) {graph[row][col] = 2;queue<pair<int, int>> que;que.push({row, col});while (!que.empty()) {int curRow = que.front().first;int curCol = que.front().second;que.pop();for (int i = 0; i < 4; i++) {int nextRow = curRow + dir[i][0];int nextCol = curCol + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1) {que.push({nextRow, nextCol});graph[nextRow][nextCol] = 2;}}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {if (graph[i][0] == 1) {bfs(graph, i, 0);}if (graph[i][M - 1] == 1) {bfs(graph, i, M - 1);}}for (int j = 0; j < M; j++) {if (graph[0][j] == 1) {bfs(graph, 0, j);}if (graph[N - 1][j] == 1) {bfs(graph, N - 1, j);}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] != 0) {graph[i][j]--;}cout << graph[i][j] << " ";}cout << "\n";}return 0;
}

103水流问题

代码随想录

#include <vector>
#include <iostream>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col) {visted[row][col] = true;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] >= graph[row][col] && !visted[nextRow][nextCol]) {dfs(graph, visted, nextRow, nextCol);}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}vector<vector<bool>> firstBoard(N, vector<bool>(M, false));vector<vector<bool>> secondBoard(N, vector<bool>(M, false));for (int i = 0; i < N; i++) {dfs(graph, firstBoard, i, 0);dfs(graph, secondBoard, i, M - 1);}for (int j = 0; j < M; j++) {dfs(graph, firstBoard, 0, j);dfs(graph, secondBoard, N - 1, j);}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (firstBoard[i][j] && secondBoard[i][j]) {cout << i << " " << j << "\n";}}}return 0;
}

104.建造最大岛屿

代码随想录

#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int count;
void dfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col, int mark) {count++;visted[row][col] = true;graph[row][col] = mark;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1 && !visted[nextRow][nextCol]) {dfs(graph, visted, nextRow, nextCol, mark);}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}bool isAllLand = true;vector<vector<bool>> visted(N, vector<bool>(M, false));unordered_map<int, int> umap;int mark = 2;for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 0) {isAllLand = false;}if (graph[i][j] == 1 && !visted[i][j]) {count = 0;dfs(graph, visted, i, j, mark);umap[mark] = count;mark++;}}}if (isAllLand) {cout << M * N;return 0;}int result = 0;unordered_set<int> vistedLand;for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 0) {count = 1;vistedLand.clear();for (int k = 0; k < 4; k++) {int nextRow = i + dir[k][0];int nextCol = j + dir[k][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}mark = graph[nextRow][nextCol];if (vistedLand.count(mark) != 0 || mark < 2) {continue;}count += umap[mark];vistedLand.insert(mark);}result = max(result, count);}}}cout << result;return 0;
}

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

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

相关文章

书生大模型实战营第三期基础岛第二课——8G 显存玩转书生大模型 Demo

8G 显存玩转书生大模型 Demo 基础任务进阶作业一&#xff1a;进阶作业二&#xff1a; 基础任务 使用 Cli Demo 完成 InternLM2-Chat-1.8B 模型的部署&#xff0c;并生成 300 字小故事&#xff0c;记录复现过程并截图。 创建conda环境 # 创建环境 conda create -n demo pytho…

[Meachines] [Easy] Legacy nmap 漏洞扫描脚本深度发现+MS08-067

信息收集 IP AddressOpening Ports10.10.10.4TCP:135,139,445 $ nmap -p- 10.10.10.4 --min-rate 1000 -sC -sV -Pn PORT STATE SERVICE VERSION 135/tcp open msrpc Microsoft Windows RPC 139/tcp open netbios-ssn Microsoft Windows n…

Docker私人学习笔记

俗话说“好记性不如烂笔头”&#xff0c;编程的海洋如此的浩大&#xff0c;养成做笔记的习惯是成功的一步&#xff01; 此笔记主要是antlr4.13版本的笔记&#xff0c;并且笔记都是博主自己一字一字编写和记录&#xff0c;有错误的地方欢迎大家指正。 一、基础概念&#xff1a;…

Tomcat 服务器详解与优化实践

文章目录 Tomcat 服务器详解与优化实践一、Tomcat 简介1.1 什么是 Tomcat1.2 Tomcat 的核心组件1.3 什么是 Servlet 和 JSP 二、Tomcat 的核心组件结构2.1 Connector2.2 Container2.3 Tomcat 请求处理过程 三、Tomcat 服务部署3.1 安装准备3.2 安装 JDK3.3 安装和启动 Tomcat3.…

Java二十三种设计模式-责任链模式(17/23)

责任链模式&#xff1a;实现请求处理的灵活流转 引言 在这篇博客中&#xff0c;我们深入探讨了责任链模式的精髓&#xff0c;从其定义和用途到实现方法&#xff0c;再到使用场景、优缺点、与其他模式的比较&#xff0c;以及最佳实践和替代方案&#xff0c;旨在指导开发者如何…

SAP BW:QUERY数据结果写入ADSO

作者 idan lian 如需转载备注出处 如果对你有帮助&#xff0c;请点赞收藏~~~ 需求背景 客户基于QUERY进行报表展示&#xff0c;现需迁移到永洪报表平台&#xff0c;query中的变量参数&#xff0c;公式等无法直接生成视图&#xff0c;query相对复杂&#xff0c;不想直接在视图…

笔记mybatisplus

MP入门 Mybatis-Plus&#xff08;简称MP&#xff09;是一个Mybatis的增强工具&#xff0c;在Mybatis的基础上只做增强不做改变&#xff0c;为简化开发、提高效率而生。 Mybatis-Plus已经封装好了大量增删改查的方法&#xff0c;程序员只需要继承BaseMapper就可以使用这些方法…

大模型从入门到实战——RAG理解

大模型从入门到实战之RAG 1. 什么是 RAG 检索增强生成&#xff08;RAG, Retrieval-Augmented Generation&#xff09; 是一种创新的模型架构&#xff0c;旨在提升大型语言模型&#xff08;LLM&#xff09;的性能和输出质量。尽管 LLM 在许多自然语言处理任务中表现出色&#…

Linux阿里云服务器,利用docker安装EMQX

第一步&#xff0c;给云服务器docker进行加速 阿里云搜索“镜像加速器”&#xff0c;找到下面这个菜单&#xff0c;点进去 然后找到镜像工具下的镜像加速器 把这个加速器地址复制 然后在自己的云服务器中&#xff0c;找到docker的文件夹 点击json配置文件 把地址修改为刚刚…

如何将LaTeX数学公式嵌入到PowerPoint中

如何将LaTeX数学公式嵌入到PowerPoint中 简介 在学术演示或技术报告中&#xff0c;清晰且专业地展示数学公式是至关重要的。PowerPoint虽然提供了一些基本的公式编辑功能&#xff0c;但如果你需要更复杂或格式严格的公式&#xff0c;使用LaTeX生成公式并嵌入到PPT中是一个极佳…

Python酷库之旅-第三方库Pandas(092)

目录 一、用法精讲 391、pandas.Series.hist方法 391-1、语法 391-2、参数 391-3、功能 391-4、返回值 391-5、说明 391-6、用法 391-6-1、数据准备 391-6-2、代码示例 391-6-3、结果输出 392、pandas.Series.to_pickle方法 392-1、语法 392-2、参数 392-3、功能…

KT来袭,打造沉浸式体验的聚合性web3应用平台

随着步入 2024&#xff0c;漫长的区块链熊市即将接近尾声。纵观产业发展&#xff0c;逆流而上往往会是彰显品牌市场影响力和技术实力的最佳证明。在这次周期中&#xff0c;一个名为KT的web3.0聚合平台吸引了市场关注&#xff0c;无论在市场层面还是技术层面&#xff0c;都广泛赢…

听劝❗用AI做职场思维导图仅仅需要几秒钟啊

本文由 ChatMoney团队出品 嘿&#xff0c;各位职场朋友们 是不是常常对着密密麻麻的笔记感到焦虑呢&#xff1f; 想整理却无从下手&#xff1f; 别怕&#xff0c;ChatmoneyAI知识库来拯救你的整理困难症啦&#xff01; 咱们都知道&#xff0c;思维导图是职场中必备的神器 …

zoom 会议机器人web例子

一、需要创建zoom app&#xff0c;创建及配置参考&#xff1a;Zoom会议机器人转写例子-CSDN博客 这里直接使用zoom-recall的配置。 二、需要生成签名&#xff0c;参数为&#xff1a;zoom-recall中的Client ID和Client Secret 1、git clone https://github.com/zoom/meetings…

【PHP入门教程】PHPStudy环境搭建+composer创建项目

文章目录 PHP 的历史PHP 的用途PHP 的特点和优势PHP 环境搭建环境准备安装window 安装CentOS / Ubuntu / Debian 安装 第一个Hello World使用Apache服务运行命令行运行代码 Composer安装 Composer&#xff1a;安装途中报错解决&#xff1a;初始化项目创建文件最终文件目录Compo…

微服务:配置管理和配置热更新

参考&#xff1a;黑马程序员之微服务 &#x1f4a5; 该系列属于【SpringBoot基础】专栏&#xff0c;如您需查看其他SpringBoot相关文章&#xff0c;请您点击左边的连接 目录 一、引言 二、配置共享 1. 添加共享配置到nacos &#xff08;1&#xff09;jdbc的共享配置 shared…

iOS开发进阶(二十三):iOS 常见面试题汇总

文章目录 1. 如何理解RunLoop2. 如何理解RunTime3. KVO与KVC有什么联系4. iOS的事件传递过程5. CALayer 与 UIView 的关系6. iOS中为什么代理需要用 weak 修饰7. Block 为什么要用 copy 修饰8. 什么是 Block9. iOS 是如何实现 APNs 的10. 谈谈对内存管理的理解11. 什么是内存池…

设计模式:Service Locator模式简介

Service Locator模式 Service Locator 模式 Service Locator模式是一种设计模式&#xff0c;属于构造型模式&#xff0c;主要用于解决对象之间的依赖关系管理。它通过提供一个集中式的服务注册和查找机制&#xff0c;使得对象可以在运行时动态地获取所需的服务实例 主要特点 …

设计模式之Decorator装饰者、Facade外观、Adapter适配器(Java)

装饰者模式 设计模式的基本原则&#xff0c;对内关闭修改。 Decorator Pattern&#xff0c;装饰者模式&#xff0c;也叫包装器模式(Wrapper Pattern)&#xff1a;将一个对象包装起来&#xff0c;增加新的行为和责任。一定是从外部传入&#xff0c;并且可以没有顺序&#xff0…

WPF 中,ControlTemplate 和 DataTemplate 是两种不同类型的模板和区别

1. 用途与定义 ControlTemplate&#xff1a;用于定义控件的外观和视觉行为。每个WPF控件都有一个ControlTemplate&#xff0c;它定义了控件的视觉树结构&#xff0c;包括控件的布局、子元素、样式以及触发器等。通过自定义ControlTemplate&#xff0c;可以彻底改变控件的外观和…