蓝桥杯每日一题2023.10.31

题目描述

全球变暖 - 蓝桥云课 (lanqiao.cn)

题目分析

果然有关连通块类的问题使用dfs都较为好写~~

我们可以通过判断连通块的代码来加上部分条件算出被完全淹没的岛屿个数

在岛屿中如果有为"#"的aij上下左右全部是"#"则说明此岛屿一定不会被完全淹没,但如果此连通块全部被遍历后发现没有这种情况则这个岛屿一定会被完全淹没,这时我们才看做它是真正我们要求的连通块,我们将其算入答案

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 2e3 + 10;
int n, cnt, ans;
char a[N][N];
bool st[N][N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void dfs(int x, int y)
{
	st[x][y] = 1;
	int o = 0;
	for(int i = 0; i < 4; i ++)
	{
		int aa = x + dx[i];
		int bb = y + dy[i];
		if(a[aa][bb] == '#')o ++;
	}
	if(o == 4)cnt ++;
	for(int i = 0; i < 4; i ++)
	{
		int aa = x + dx[i];
		int bb = y + dy[i];
		if(a[aa][bb] == '#' && !st[aa][bb])dfs(aa, bb);
	}
}

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= n; j ++)
		{
			cin >> a[i][j];
		}
	}
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= n; j ++)
		{
			if(!st[i][j] && a[i][j] == '#')
			{
				cnt = 0;
				dfs(i, j);
				if(cnt == 0)ans ++;
			}	
		}
	}
	cout << ans;
	return 0;
}
相关推荐
海绵宝宝转agent9 小时前
JUC高频面试八股总结个人开源
面试·职场和发展
Nil20810 小时前
leetcode 118杨辉三角
算法·leetcode·职场和发展
我命由我1234514 小时前
Photoshop - Photoshop 使用内容识别填充移除对象
学习·职场和发展·产品运营·求职招聘·职场发展·学习方法·photoshop
我命由我1234517 小时前
Photoshop - Photoshop 使用简单的选择工具
学习·职场和发展·产品运营·求职招聘·职场发展·学习方法·photoshop
我命由我1234518 小时前
Photoshop - Photoshop 使用工具
学习·职场和发展·产品运营·求职招聘·职场发展·产品经理·学习方法
cc57250265318 小时前
2027 秋招|大数据 / 统计类专业运营管培群面,数据思维落地方法
面试·职场和发展
我命由我1234519 小时前
Photoshop - Photoshop 设计概念
学习·职场和发展·产品运营·求职招聘·职场发展·产品经理·学习方法
Pniubi19 小时前
力扣41缺失的第一个正数(哈希表法)
算法·leetcode·职场和发展
IT大白鼠20 小时前
图数据库系列 · 第 08 篇——面试收官:高频题与全景总结
数据库·面试·职场和发展·nosql·图数据库
我命由我123451 天前
Photoshop - Photoshop 使用魔棒工具选择单独的区域
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop