蓝桥杯每日一题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;
}
相关推荐
Interview Aid11214 小时前
OpenAI SWE 面试流程
面试·职场和发展
我命由我1234515 小时前
Photoshop - Photoshop 使用对齐功能定位元素
学习·职场和发展·产品运营·求职招聘·职场发展·产品经理·学习方法
ychqsq16 小时前
212.涟漪
经验分享·职场和发展
ychqsq16 小时前
213.取舍
经验分享·职场和发展
夏幻灵16 小时前
每日一题:LeetCode 3:无重复字符的最长子串——滑动窗口思路详解
算法·leetcode·职场和发展
智者知已应修善业18 小时前
【51单片机只有一个8*8点阵数据如何滚动起来一片595+一组IO+花样】2022-1-15
c++·经验分享·笔记·单片机·蓝桥杯·硬件架构
考研保研资料分享20 小时前
电子信息保研经验合辑:复旦直博与北邮集成电路,夏令营、预推免和专业面试
数学建模·面试·职场和发展·信息与通信
我命由我123452 天前
Photoshop - Photoshop 矢量蒙版
学习·职场和发展·产品运营·求职招聘·职场发展·产品经理·photoshop
我命由我123452 天前
Photoshop - Photoshop 面板和菜单
学习·职场和发展·产品运营·求职招聘·职场发展·产品经理·photoshop
我命由我123452 天前
Photoshop - Photoshop 使用仿制图章工具移除对象
学习·职场和发展·产品运营·求职招聘·职场发展·产品经理·学习方法