acwing1233.全球变暖

news/2025/3/20 12:56:36/

算法:Flood Fill

bfs / dfs

统计被完全淹没的岛屿

两种方法:

1. 使用 total 和  bound 记录岛屿格子的数量和被淹没的格子数量,如果 bound == total,说明这个岛屿被完全淹没了。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>using namespace std; typedef pair<int, int> PII;
#define x first
#define y secondconst int N = 1010;int n;
bool st[N][N];
char s[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};void bfs(PII start, int& total, int& bound) { //使用引用可以影响原变量queue<PII> q;q.push(start);st[start.x][start.y] = true;while (!q.empty()) {PII t = q.front();q.pop();total ++ ;//每次有一个格子出栈总数量加一bool is_bound = false;for (int i = 0; i < 4; i++){int x = t.x + dx[i], y = t.y + dy[i];if (x < 0 || x >= n || y < 0 || y >= n) continue;if (st[x][y]) continue;if (s[x][y] == '.'){is_bound = true;//周围有海洋则标记当前格子continue;}st[x][y] = true;q.push({x, y});}if(is_bound) bound ++ ;//当前格子被标记了则bound++}
}int main() {cin >> n;for (int i = 0; i < n; i++) cin >> s[i];// 原始岛屿数量int cnt = 0;for (int i = 0; i < n; i++){for (int j = 0; j < n; j++)if (s[i][j] == '#' && !st[i][j]){int total = 0, bound = 0;bfs({i, j}, total, bound);if(total == bound) cnt ++ ;}}cout << cnt << endl;return 0;
}

2. 使用vector<vector<PII>> islands 记录岛屿数量  vector<PII>记录每个岛屿内部的格子 ,模拟一遍淹没的过程将岛屿变成海洋,如果一个岛屿内部所有的格子都别淹没了res ++ ;(学习vector的使用方法)

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>using namespace std;typedef pair<int, int> PII;
#define x first
#define y secondconst int N = 1010;int n;
bool st[N][N];
char g[N][N];  // 改用g存储原始地图
bool drown[N][N];  // 淹没标记数组
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
vector<vector<PII>> islands;  // 存储所有原始岛屿void bfs(PII start, vector<PII>& island) {queue<PII> q;q.push(start);st[start.x][start.y] = true;island.push_back(start);while (!q.empty()) {auto t = q.front();q.pop();for (int i = 0; i < 4; i++) {int a = t.x + dx[i], b = t.y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n) continue;if (st[a][b] || g[a][b] != '#') continue;st[a][b] = true;q.push({a, b});island.push_back({a, b});}}
}int main() {cin >> n;for (int i = 0; i < n; i++) cin >> g[i];// BFS:记录所有原始岛屿for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (g[i][j] == '#' && !st[i][j]) {vector<PII> island;bfs({i, j}, island);islands.push_back(island);}}}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (g[i][j] != '#') continue;for (int k = 0; k < 4; k++) {int a = i + dx[k], b = j + dy[k];if (a >= 0 && a < n && b >= 0 && b < n && g[a][b] == '.') {drown[i][j] = true;break;}}}}// 统计完全被淹没的岛屿数量int res = 0;for (auto& island : islands) {bool all_drowned = true;for (auto& p : island) {if (!drown[p.x][p.y]) {  // 存在未被淹没的陆地all_drowned = false;break;}}if (all_drowned) res++;}cout << res << endl;return 0;
}


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

相关文章

【合新通信】---射频光模块

射频光模块&#xff08;RF Optical Module&#xff09;是一种将射频信号与光信号相互转换的设备&#xff0c;广泛应用于无线通信、雷达、卫星通信、光纤通信等领域。以下是关于射频光模块的详细介绍&#xff1a; 一、射频光模块的基本概念 定义&#xff1a; 射频光模块是一种将…

第七章 排序算法法法

算法时间复杂度 衡量一个算法的时间复杂度 度量一个程序(算法)执行时间的两种方法 事后统计法 这种方法可行,但是有两个问题:一是要想对涉及的算法的运行性能进行评测,需要实际运行该程序;二是所得时间的统计量依赖于计算机的硬件,软件等环境因素,这种方式,要在同一台计算机的…

涨薪技术|Kubernetes(k8s)之yaml语法大全

01yaml介绍 YAML 语言(发音 /ˈjməl/ )的设计目标&#xff0c;就是方便人类读写。YAML代表YAML Aint Markup Language,是一种数据序列化语言。它实质上是一种通用的数据串行化格式,它的基本语法规则如下。 大小写敏感; 使用缩进表示层级关系; 缩进时不允许使用Tab键&#xf…

html实现table超出宽度后滑动展示

需求:这是一个详情页面,table等标签都是在后台录入的,要求实现table表格超出屏幕宽度后,可以左右滑动展示的效果。 .knowledgeDetails table{overflow: hidden;height: auto !important;width: 100%

3.数据探索与可视化基本图形(直方图、箱线图、散点图)——Python数据挖掘代码实践

文章目录 一、 基本概念与专业术语解析1.1 数据分布、相关性与多维数据1.2 专业术语解释与图形介绍 二、 直方图2.1 使用 Matplotlib 绘制基础直方图2.2 使用 Seaborn 绘制直方图 密度曲线2.3 不同 bin 规则对比 三、 箱线图3.1 理论基础3.2 绘制箱线图3.2.1 使用 Matplotlib …

【MySQL篇】复合查询

目录 前言&#xff1a; 1&#xff0c;多表查询 2&#xff0c;自连接 3&#xff0c;子查询 3.1&#xff0c;单行子查询 3.2&#xff0c;多行子查询 3.3&#xff0c;多列子查询 3.3&#xff0c;在from子句中使用子查询 4&#xff0c;合并查询 4.1&#xff0c;union …

leetcode974. 和可被 K 整除的子数组

思路 使用前缀数组可以快速统计加和问题。然后基于题目&#xff0c;考虑是寻找整除的子集&#xff0c;换个说法&#xff0c;当前前缀的余数要与之前的某个余数一样&#xff0c;两前缀之差为合格子集。 除此外&#xff0c;额外统计前缀中本身就余数为0的子集数量。 class Solu…

实验5:Vuex状态管理

Web前端开发技术课程实验报告 实验5&#xff1a;Vuex状态管理 一、实验目的&#xff1a; 掌握Vuex的工作原理和5个核心概念。掌握Vuex API接口的使用方法。 二、实验要求&#xff1a; 掌握mutations、actions、getters的定义和使用方法&#xff0c;完成以下实验内容。上交实…