#1241: Oil Deposits(八向搜索 + 并查集)
Oil Deposits
It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large
and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
1
2
2
本题是指@表示油点,*表示非油点,油点相邻则表示一个油点而不是多个,找出总油点个数。没啥技巧,就是八个方向搜索,同时利用并查集的效率更高
DFS搜索:
#include<bits/stdc++.h>
using namespace std;
const int N = 100 + 5;
char g[N][N];
bool book[N][N];
int n, m;
const int dx[] = { 0,0,1,1,1,-1,-1,-1 };
const int dy[] = { 1,-1,0,1,-1,0,1,-1 };
void dfs(int i, int j) {
book[i][j] = 1;
for (int k = 0; k < 8; k++){
int nx = i + dx[k], ny = j + dy[k];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && book[nx][ny] == 0 && g[nx][ny] == '@')dfs(nx, ny);
}
}
int main() {
//freopen("in.txt", "r", stdin);
while (cin >> m >> n && n && m) {
memset(g, 0, sizeof(g));
memset(book, 0, sizeof(book));
for (int i = 0; i < m; i++)cin >> g[i]; int cnt = 0;
for (int i = 0; i < m; ++i)for (int j = 0; j < n; ++j)if (g[i][j] == '@' && !book[i][j]) { cnt++; dfs(i, j); }
cout << cnt << endl;
}
return 0;
}
并查集:
#include<bits/stdc++.h>
using namespace std;
char g[101][101];
int fa[101 * 101];
int x[] = { 0,0,1,-1,1,-1,1,-1 };
int y[] = { 1,-1,0,0,1,1,-1,-1 };
int n, m;
int id(int x, int y) {
return (x - 1)*m + y;
}
bool isok(int x, int y) {
if (0 < x&&x <= n && 0 < y&&y <= m)
return true;
return false;
}
int Find(int x) {
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
int vis[101 * 101];
int main() {
while (scanf("%d%d", &n, &m) != EOF && n + m) {
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
fa[id(i, j)] = id(i, j);
}
}
for (int i = 1; i <= n; ++i)
scanf("%s", g[i] + 1);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (g[i][j] == '*') continue;
for (int k = 0; k < 7; ++k) {
int xx = i + x[k], yy = j + y[k];
if (isok(xx, yy) == false || g[xx][yy] == '*') continue;
int f1 = Find(id(i, j)), f2 = Find(id(xx, yy));
if (f1 != f2) {
fa[f1] = f2;
}
}
}
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (g[i][j] != '*'&&vis[Find(id(i, j))] == 0) {
cnt++;
vis[Find(id(i, j))] = 1;
}
}
}
cout << cnt << endl;
}
return 0;
}
#1241: Oil Deposits(八向搜索 + 并查集)的更多相关文章
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- hdu 1241 Oil Deposits (简单搜索)
题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- hdoj 1241 Oil Deposits
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1241 Oil Deposits (DFS)
题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...
- HDU 1241 Oil Deposits【DFS】
解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...
随机推荐
- 【MRCTF2020】Ezpop_Revenge——PHP原生类SSRF
[MRCTF2020]Ezpop_Revenge--PHP原生类SSRF 1. 收获 CMS初审计 google.baidu hack PHP原生类反序列化 2. 看题 2.1 读源码 网页存在源码泄 ...
- [ABC262F] Erase and Rotate
Problem Statement You are given a sequence $P = (p_1,p_2,\ldots,p_N)$ that contains $1,2,\ldots,N$ e ...
- 一个Servlet如何实现增-删-改-查的业务逻辑
一.业务场景 最近在教学生学习JavaWeb中的Servlet,它就是一个Java服务端的小程序,用来提供各种服务. 在讲解得时候,自己突然遇到一个问题,那就是现在没有使用什么SpringMvc框架, ...
- 高斯朴素贝叶斯(Gaussian Naive Bayes)原理与实现——垃圾邮件识别实战
朴素贝叶斯(Naive Bayes): 根据贝叶斯定理和朴素假设提出的朴素贝叶斯模型. 贝叶斯定理: 朴素假设(特征条件独立性假设): 代入可知朴素贝叶斯模型计算公式: 因为朴素贝叶斯是用来分类任务, ...
- python自动化测试相关资料
java神功: https://yuedu.baidu.com/ebook/10f4bf7530126edb6f1aff00bed5b9f3f80f7212 selenium书:https:// ...
- kafka源码阅读之MacBook Pro M1搭建Kafka2.7版本源码运行环境
原创/朱季谦 最近在阅读Kafka的源码,想可以在阅读过程当中,在代码写一些注释,便决定将源码部署到本地运行. 日常开发过程中,用得比较多一个版本是Kafka2.7版本,故而在MacBook Pro笔 ...
- thymeleaf自定义标签
前言 使用thymeleaf自定义标签,环境:springboot 2.3.7 + thymeleaf 3.0.11(2021-01-14最新版) 由于使用shiro,我们需要与thymeleaf整合 ...
- 文心一言 VS 讯飞星火 VS chatgpt (56)-- 算法导论6.3 2题
文心一言 VS 讯飞星火 VS chatgpt (56)-- 算法导论6.3 2题 对于 BUILD-MAX-HEAP 中第 2行的循环控制变量 i 来说,为什么我们要求它是从A.length/2到1 ...
- 2023-05-13:你现在手里有一份大小为 n x n 的 网格 grid, 上面的每个 单元格 都用 0 和 1 标记好了其中 0 代表海洋,1 代表陆地。 请你找出一个海洋单元格,这个海洋单元格
2023-05-13:你现在手里有一份大小为 n x n 的 网格 grid, 上面的每个 单元格 都用 0 和 1 标记好了其中 0 代表海洋,1 代表陆地. 请你找出一个海洋单元格,这个海洋单元格 ...
- 初窥门径代码起手,Go lang1.18入门精炼教程,由白丁入鸿儒,首次运行golang程序EP01
前文再续,书接上回,前一篇:兔起鹘落全端涵盖,Go lang1.18入门精炼教程,由白丁入鸿儒,全平台(Sublime 4)Go lang开发环境搭建EP00,我们搭建起了Go lang1.18的开发 ...