Oil Deposits

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 12461 Accepted Submission(s): 7245

Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots.
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.

Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100
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.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output

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(八向搜索 + 并查集)的更多相关文章

  1. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. hdu 1241 Oil Deposits (简单搜索)

    题目:   The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...

  3. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  4. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  5. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  6. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  7. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  8. hdoj 1241 Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

  10. HDU 1241 Oil Deposits【DFS】

    解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...

随机推荐

  1. Python将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

    def SlowSnail(n): while n != 1: # 循环保证递归 for index in range(2, n + 1): if n % index == 0: n //= inde ...

  2. Snipaste安装与使用教程

    一.下载 1.官网 下载地址 2.下载 可以直接在官网下载,也可以跳转微软仓库下载增强版的.如果是其他系统,mac在最下面,而Linux现在没有需要等待. 二.开始安装 1.下载完毕,是一压缩包绿色版 ...

  3. Redis 学习笔记2:持久化

    目录 1 什么是持久化 1.1 aof 1.2 rdb 2 RDB持久化 2.1 RDB 是什么 2.2 手动触发 3 AOF持久化 3.1 aof 是什么 3.2 appendfile 文件说明: ...

  4. MD5在文件安全中的应用与重要性

    一.MD5简介 MD5(Message-Digest Algorithm 5)是一种广泛应用的密码散列函数,由美国密码学家罗纳德·李维斯特(Ronald Linn Rivest)于1992年提出.它主 ...

  5. 组合式api-通过reactive和ref提供响应式数据

    在setup中如果是直接定义遍历数据并不是响应式数据,和vue2中的data选项提供的数据不一样,vue2的data中返回的数据全部都是响应式数据. <script setup> // 这 ...

  6. ElasticSearch索引库的增删改查

    官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/6.8/indices.html 创建索引.设置分片 https://www. ...

  7. 【C#】【DateTime】获取当前时间的前一天或者后一天方法学习

    假期懈怠了一阵子,因为工作环境没有网络,随即记录下当时的问题: 1.关于DateTime.Now(2023/10/07)的前一天的时间(2023/10/06),想要通过ToString转换成字符串后除 ...

  8. JMeter变量和调试取样器

    变量 可以在测试计划中定义变量 可以通过${变量名}进行调用 调试取样器可以看到变量参数

  9. python 处理pdf加密文件

    近期有同事需要提取加密的pdf文件,截取其中的信息,并且重构pdf文件.网上没有搜到相关的pdf操作,于是咨询了chatgpt,给出了pypdf2的使用案例.但是时间比较久远了,很多库内的调用接口都已 ...

  10. 18、Scaffold 布局组件

    Scaffold 是一个非常重要的布局组件,它提供了一个常见的应用程序结构布局,包含了许多基础的 UI 元素和交互方式. Scaffold 组件主要由以下几个部分组成: AppBar:用于显示在页面顶 ...