AcWing 1233. 全球变暖【FloodFill】【BFS】【DFS】
AcWing 1233. 全球变暖
- 一、题目链接
- 二、题目分析
- (一)算法标签
- (二)解题思路
- 三、AC代码
- 解法一(BFS):
- 解法二(DFS):
- 四、其它题解
一、题目链接
AcWing 1233. 全球变暖
二、题目分析
(一)算法标签
FloodFill BFS DFS
(二)解题思路
第一步:计算出有多少个连通块(岛屿)
一般而言,找连通块的方法有两种,第一种是遍历(包括DFS 和BFS),第二种是并查集。
这里以bfs为例,具体如下:
遍历每一个点,如果没有被访问过并且是#的话,那么bfs搜索一下,同时cnt++
第二步:计算出有多少个会被完全淹没,这里与上一步同时求了
详细了解搜索类题目(DFS、BFS),请移步DFS BFS 搜索题目归纳
三、AC代码
解法一(BFS):
#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;char g[N][N];
bool st[N][N];
int n;int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};// 判断周围是否都是岛屿#
bool check(int x, int y)
{int cnt = 0;for (int i = 0; i < 4; i ++ ){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n) continue;if (g[a][b] == '#')cnt ++ ;}if (cnt == 4) return true;else return false;
}
bool bfs(int x, int y)
{st[x][y] = true;queue<PII> q;q.push({x, y});int cnt = 0;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 (g[a][b] == '.') continue;if (!st[a][b] && check(a, b)) cnt ++ ;if (!st[a][b] && g[a][b] == '#'){st[a][b] = true;q.push({a, b});}}}if (cnt) return false;else return true;
}
int main()
{cin >> n;for (int i = 0; i < n ; i ++ ) scanf("%s", g[i]);int res = 0;for (int i = 0; i < n; i ++ )for (int j = 0; j < n; j ++ ){if (!st[i][j] && g[i][j] == '#'){if (bfs(i, j)) res ++ ;}}cout << res << endl;return 0;
}
解法二(DFS):
#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;char g[N][N];
bool st[N][N], flag;
int n;int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};// 判断周围是否都是岛屿#
bool check(int x, int y)
{int cnt = 0;for (int i = 0; i < 4; i ++ ){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n) continue;if (g[a][b] == '#')cnt ++ ;}if (cnt == 4) return true;else return false;
}void dfs(int x, int y)
{// cout << x << ' ' << y << endl;st[x][y] = true;if (check(x, y)) flag = true;for (int i = 0; i < 4; i ++ ){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n) continue;if (g[a][b] == '.') continue;// if (!st[a][b] && g[a][b] == '#' && check(a, b)) return false;if (!st[a][b] && g[a][b] == '#'){dfs(a, b);// st[a][b] = false;}}
}int main()
{cin >> n;for (int i = 0; i < n ; i ++ ) scanf("%s", g[i]);int res = 0;for (int i = 0; i < n; i ++ )for (int j = 0; j < n; j ++ ){if (!st[i][j] && g[i][j] == '#'){flag = false;dfs(i, j);if (!flag) res ++ ;}}cout << res << endl;return 0;
}
下面给出dfs错误代码(只过了1个测试用例):
错误原因:dfs函数的开头check判断直接返回,可能导致该连通块还没搜完就返回了,导致下一次dfs还是在该连通块内
#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;char g[N][N];
bool st[N][N];
int n;int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};// 判断周围是否都是岛屿#
bool check(int x, int y)
{int cnt = 0;for (int i = 0; i < 4; i ++ ){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n) continue;if (g[a][b] == '#')cnt ++ ;}if (cnt == 4) return true;else return false;
}bool dfs(int x, int y)
{// cout << x << ' ' << y << endl;st[x][y] = true;if (check(x, y)) return false;for (int i = 0; i < 4; i ++ ){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n) continue;if (g[a][b] == '.') continue;// if (!st[a][b] && g[a][b] == '#' && check(a, b)) return false;if (!st[a][b] && g[a][b] == '#'){return dfs(a, b);// st[a][b] = false;}}return true;
}int main()
{cin >> n;for (int i = 0; i < n ; i ++ ) scanf("%s", g[i]);int res = 0;for (int i = 0; i < n; i ++ )for (int j = 0; j < n; j ++ ){if (!st[i][j] && g[i][j] == '#'){if (dfs(i, j)) res ++ ;}}cout << res << endl;return 0;
}
四、其它题解
AcWing 1233. 全球变暖 1
AcWing 1233. 全球变暖 2
y总手写队列+bfs
AcWing 1233. 全球变暖【FloodFill】【BFS】【DFS】相关推荐
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- [Leetcode][第785题][JAVA][判断二分图][BFS][DFS]
[问题描述][中等] [解答思路] 1. DFS 深度优先遍历 时间复杂度:O(N+M) 空间复杂度:O(N) class Solution {private static final int UNC ...
- LeetCode 1263. 推箱子(BFS+DFS / 自定义哈希set)
文章目录 1. 题目 2. 解题 2.1 超时解 2.2 BFS + DFS 1. 题目 「推箱子」是一款风靡全球的益智小游戏,玩家需要将箱子推到仓库中的目标位置. 游戏地图用大小为 n * m 的网 ...
- LeetCode 1034. 边框着色(BFS/DFS)
文章目录 1. 题目 2. 解题 2.1 BFS 2.2 DFS 1. 题目 给出一个二维整数网格 grid,网格中的每个值表示该位置处的网格块的颜色. 只有当两个网格块的颜色相同,而且在四个方向中任 ...
- 全局路径规划:图搜索算法介绍1(BFS/DFS)
对于全局路径规划的设计,我们先要了解什么是图搜索,在此之前,要先知道什么是图: 可以看到,图有很多种,有无向图,有向图,节点之间还可以有不同的weight, 用于表述从节点与节点直接迁移的代价. 而图 ...
- 深度搜索 java_java实现的深度搜索与广度搜索算法BFS,DFS以及几种最短路径算法...
java实现的深度搜索与广度搜索算法BFS,DFS以及几种最短路径算法 public class City { String name; int id; static int idCounter = ...
- 【八数码问题】基于状态空间法的知识表示与状态搜索:无信息搜索(BFS/DFS) 启发式搜索(A*)
前言 一.问题引入 二.状态空间法 1. 知识及其表示 2. 状态空间法定义 3. 问题求解 三.基于状态空间搜索法解决八数码问题 1. 八数码问题的知识表示 2. 状态空间图搜索 1. 无信息搜索 ...
- bfs dfs 搜索入门模板题
bfs & dfs 题目链接:https://vjudge.net/contest/404511 1.最短路(bfs) (1)一维最短路 D - Catch That Cow 题目大意: 在一 ...
- 动态规划+BFS+DFS+回溯+红黑树+排序+链表+位运算(B站优质学习资源链接,后续会继续更新)
动态规划 正月点灯笼(UP主) 个人主页 https://space.bilibili.com/24014925/channel/detail?cid=12580 动态规划第一讲 https://ww ...
最新文章
- 谷歌让NLP模型也能debug,只要给一张「草稿纸」就行
- Java并发编程实战系列10之避免活跃性危险
- 准备入门IC的全局观念系列-下
- 专栏 | 基于 Jupyter 的特征工程手册:特征选择(五)
- helm部署Loki
- Oracle Recyclebin
- 雅虎将于4月底关闭自助式广告测试
- 切换表达式到Java吗?
- 递归调用、高阶函数、装饰器
- android mimetype类型,如何确定android中的MIME类型?
- scoped_ref
- paip.重装系统需要备份的资料总结..v2.0 cad
- xshell无法删除输入_xshell5卸载不了怎么办?xshell5软件卸载方法及无法卸载的解决方法...
- Python-经验模态分解(EMD)分解及测试用例
- 明日方舟泰拉世界解析
- html中统计图怎么做,如何做专业、美观的图表(基础统计图部分)
- LiveZilla 详细 配置 设置 (三) 配置 LiveZilla 服务
- h5 app跳转客服咨询 临时会话 (没有开通在线咨询、无法会话)
- 不同应用选择荧光染料 -CY7 ALK脂溶性Sulfo-Cyanine7 alkyne 结构式应用
- 将软件部署到云端的可行性及其应用意义,是什么?