当前位置: 首页 > article >正文

每日一题洛谷P8662 [蓝桥杯 2018 省 AB] 全球变暖c++

P8662 [蓝桥杯 2018 省 AB] 全球变暖 - 洛谷 (luogu.com.cn)

DFS

#include<iostream>
using namespace std;
int n, res;
char a[1005][1005]; bool vis[1005][1005];
bool flag;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
void dfs(int x, int y) {vis[x][y] = true;bool safe = false;for (int i = 0; i < 4; i++) {int xx = x + dx[i];int yy = y + dy[i];if (xx<1 || yy<1 || xx>n || yy>n || a[xx][yy] == '.') {safe = true;break;}}if (!safe)flag = true;for (int i = 0; i < 4; i++) {int xx = x + dx[i];int yy = y + dy[i];if (xx >= 1 && yy >= 1 && x <= n && yy <= n && a[xx][yy] == '#' && !vis[xx][yy]) {dfs(xx, yy);}}
}
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 (a[i][j] == '#' && !vis[i][j]) {flag = false;vis[i][j] = true;dfs(i, j);if (!flag)res++;}flag = false;}}cout << res;return 0;
}

BFS

#include<iostream>
using namespace std;
#include<queue>
int n, res;
char a[1001][1001]; bool vis[1001][1001];
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
bool bfs(int x, int y) {vis[x][y] = true;queue<pair<int, int>> q;bool flag = false;q.push({ x,y });while (!q.empty()) {pair<int, int> t = q.front();q.pop();bool safe = false;for (int i = 0; i < 4; i++) {int xx = t.first + dx[i];int yy = t.second + dy[i];if (xx<1 || yy<1 || xx>n || yy>n || a[xx][yy] == '.') {safe = true;break;}}if (!safe)flag = true;for (int i = 0; i < 4; i++) {int xx = t.first + dx[i];int yy = t.second + dy[i];if (xx >= 1 && yy >= 1 && xx <= n && yy <= n && a[xx][yy] == '#' && !vis[xx][yy]) {vis[xx][yy] = true;q.push({ xx,yy });}}}return !flag;
}
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 (a[i][j] == '#' && !vis[i][j]) {if (bfs(i, j))res++;}}}cout << res;return 0;
}

http://www.lryc.cn/news/2392351.html

相关文章:

  • 【JVM】初识JVM 从字节码文件到类的生命周期
  • 多级体验体系构建:基于开源AI智能客服与AI智能名片的S2B2C商城小程序体验升级路径研究
  • 每日算法 -【Swift 算法】字符串转整数算法题详解:myAtoi 实现与正则表达式对比
  • 记录一个难崩的bug
  • Git切换历史版本及Gitee云绑定
  • 智能外呼系统中 NLP 意图理解的工作原理与技术实现
  • 服务器的IP是什么东西?
  • [问题解决]:Unable to find image ‘containrrr/watchtower:latest‘ locally
  • 【文件上传】阿里云对象存储服务实现文件上传
  • IPv6代理如何引领下一代网络未来
  • Linux——数据链路层
  • ubuntu 22.04 安装下载
  • 深度学习面试八股简略速览
  • 【深度学习-pytorch篇】1. Pytorch矩阵操作与DataSet创建
  • 游戏引擎学习第310天:利用网格划分完成排序加速优化
  • 数据结构 - 树的遍历
  • 时序模型介绍
  • Java面试实战:从Spring到大数据的全栈挑战
  • 解决idea与springboot版本问题
  • 【第4章 图像与视频】4.4 离屏 canvas
  • [AXI]如何验证AXI5原子操作
  • 尚硅谷redis7 74-85 redis集群分片之集群是什么
  • Android获取设备信息
  • WPF的基础控件:布局控件(StackPanel DockPanel)
  • apache的commons-pool2原理与使用详解
  • 打印Yolo预训练模型的所有类别及对应的id
  • 语法糖介绍(C++ Python)
  • 事务详解及面试常考知识点整理
  • 设计模式26——解释器模式
  • 在MDK中自动部署LVGL,在stm32f407ZGT6移植LVGL-8.3,运行demo,显示label