蓝桥杯每日一题2023.10.31

题目描述

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

题目分析

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

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

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

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;
}
相关推荐
aaaweiaaaaaa1 小时前
c++基础学习(学习蓝桥杯 ros2有C基础可看)
c++·学习·蓝桥杯·lambda·ros2·智能指针·c++类
汉克老师2 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(4、最大空白区)
c++·算法·蓝桥杯·蓝桥杯c++·c++蓝桥杯
007php0074 小时前
某大厂MySQL面试之SQL注入触点发现与SQLMap测试
数据库·python·sql·mysql·面试·职场和发展·golang
JosieBook1 天前
【程序人生】有梦想就能了不起,就怕你没梦想
程序人生·职场和发展
scx201310041 天前
P13929 [蓝桥杯 2022 省 Java B] 山 题解
c++·算法·蓝桥杯·洛谷
data myth1 天前
力扣1210. 穿过迷宫的最少移动次数 详解
算法·leetcode·职场和发展
Greedy Alg1 天前
LeetCode 240. 搜索二维矩阵 II
算法·leetcode·职场和发展
墨染点香1 天前
LeetCode 刷题【68. 文本左右对齐】
算法·leetcode·职场和发展
GalaxyPokemon1 天前
LeetCode - 202. 快乐数
算法·leetcode·职场和发展
吃着火锅x唱着歌1 天前
LeetCode 522.最长特殊序列2
算法·leetcode·职场和发展