Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45017    Accepted Submission(s): 25972

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
 
Source
 
Recommend
Eddy
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
本题应为DFS的题目
但由于博主学DFS学的比较菜
 
所以博主没有采用DFS
而是采用了多次BFS的方式
逐行逐列搜索map
一旦找到一个@就向八个方向BFS一次
每次BFS的时候把@变为*
这样搜完整片map
就结束
下面上我全是注释
谁都看的懂的代码
 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
int dir[][]={{-,},{,},{,-},{,},{-,-},{-,},{,},{,-}};//此处应为八个方向
int n,m;
char a[][];
struct node
{
int x,y;
};
void bfs(int x,int y)
{
int i;
queue<node> q;
node g;
g.x=x;g.y=y;
q.push(g);
a[x][y]='*';
while(!q.empty())
{
node t=q.front();
q.pop();
for(i=;i<;i++)//八个方向寻找是否成片
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<m&&dy<n&&a[dx][dy]=='@')//如果出现@,变为*(防止下面重复运算)
{
a[dx][dy]='*';
g.x=dx;g.y=dy;
q.push(g);
}
}
}
}
int main()
{
int x;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==&&n==)
return ;
x=;//初始化
for(int i=;i<m;i++)
scanf("%s",a[i]);
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
if(a[i][j]=='@')//找到一个@就bfs一下是否成一片,并且消除它们
{
bfs(i,j);
x++;
}
}
}
printf("%d\n",x);
}
return ;
}
 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Notes:DFS的方法暂时还没有想好,但博主认为这样的方法更好
2018-11-20  02:11:33  Author:LanceYu

HDU 1241 Oil Deposits 题解的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. HDU 1241 Oil Deposits【DFS】

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

  8. hdu 1241:Oil Deposits(DFS)

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

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

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

随机推荐

  1. vue2.0教手架安装

  2. 【洛谷P4251】[SCOI2015]小凸玩矩阵(二分+二分图匹配)

    洛谷 题意: 给出一个\(n*m\)的矩阵\(A\).现要从中选出\(n\)个数,任意两个数不能在同一行或者同一列. 现在问选出的\(n\)个数中第\(k\)大的数的最小值是多少. 思路: 显然二分一 ...

  3. OC方法交换swizzle详细介绍——不再有盲点

    原文链接:https://www.cnblogs.com/mddblog/p/11105450.html 如果对方法交换已经比较熟悉,可以跳过整体介绍,直接看常见问题部分 整体介绍 方法交换是runt ...

  4. H5/纯JS实现:把网页中的文字复制到剪切板

    copy =() => { const dom = document.getElementById(`collect-text-${t.Id}`) const selection = windo ...

  5. 【2019.8.11上午 慈溪模拟赛 T2】十七公斤重的文明(seventeen)(奇偶性讨论+动态规划)

    题意转化 考虑我们对于集合中每一个\(i\),若\(i-2,i+k\)存在,就向其连边. 那么,一个合法的集合就需要满足,不会存在环. 这样问题转化到了图上,就变得具体了许多,也就更容易考虑.求解了. ...

  6. C++ TCP客户端网络消息发送接收同步实现

    废话不多说, 直入主题, 我们在写客户单的时候希望在哪里发消息出去,然后在哪里返回消息(同步), 然后继续往下运行-, 而不是在这里发送了一个消息给服务端, 在另一个地方接受消息(异步) , 也不知道 ...

  7. 编写antd配置表单组件

    编写antd配置表单组件 整体思路 抽取formitem的配置数组:包含组件类型.名称.label名称,相关的opts和扩展的opts,传递进入组件 组件通过Form.create()进行表单创建,能 ...

  8. ubuntu16.04跑通Mask R-CNN Demo

    1. 下载源码: git clone https://github.com/matterport/Mask_RCNN 2. 安装依赖项(其实就是程序的运行环境) 我是用conda新建的虚拟环境. (1 ...

  9. Kubernetes SatefulSet(有状态应用部署)

    Kubernetes SatefulSet(有状态应用部署) • 部署有状态应用• 解决Pod独立生命周期,保持Pod启动顺序和唯一性1. 稳定,唯一的网络标识符,持久存储2. 有序,优雅的部署和扩展 ...

  10. Vue基础框架

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!-- 设置语言为 ...