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. JSP中 JSTL和EL标签的使用

    使用JSTL前的准备 想要使用JSTL,首先需要给工程导入JSTL的包(JSTL.jar和standard.jar). JSTL简介 JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应 ...

  2. web-never give up

    打开题目连接 ?id=1 ,疑是注入点 但是输入其他数字无果 打开源码,发现注释有网页链接 打开连接123.206.87.240:8006/test/1p.html 发现回到了bugku的论坛首页,应 ...

  3. django获取某一个字段的列表 values values_list flat=true

    1.values() print(Question.objects.values('title')) #得到的是一个字典 <QuestionQuerySet [{'title': '查询优化之s ...

  4. pandas 排序之 sort_values,reindex,reset_index, sort_index

    如果想按照自己的方式排序ind = 行索引data= data[ind] ind = data.sum(axis=1).sort_values(ascending=False).index data ...

  5. 调试九法: 软硬件错误的排查之道 (David J. Agans 著)

    第1章 简介 (已看) 第2章 总体规则 (已看) 第3章 理解系统 (已看) 第4章 制造失败 第5章 不要想, 而要看 第6章 分而治之 第7章 一次只改一个地方 第8章 保持审计跟踪 第9章 检 ...

  6. 在Ubuntu18.04.2LTS上安装搜狗输入法

    在Ubuntu18.04.2LTS上安装搜狗输入法 一.前言 最近项目使用到了Linux系统,因此就安装了Ubuntu18.04.2这个最新的LTS的OS.整体的使用效果是不敢恭维的,特别是使用虚拟机 ...

  7. weblogic 12c 安装与下载

    转   一.WebLogic的介绍     WebLogic是美国bea公司出品的一个application server,确切的说是一个基于Javaee架构的中间件,纯java开发的,最新版本Web ...

  8. SpringBoot整合log4j2导入新的依赖出现jar冲突解决

    1.问题复现: 之前在SpringBoot中配置整合了log4j2,今天在pom文件中,导入新的依赖(依赖如下)之后, <dependency> <groupId>com.gi ...

  9. npm和yarn的区别,我们该如何选择?

    首先,这两个都属于js包管理工具,都可以安装包或者模块yarn 是由facebook.google等联合开发推出的区别: npm 下载包的话 比如npm install它是按照包的排序,也就是队列挨个 ...

  10. 使用Node.js时如何引入jQuery

    使用Node.js时如何引入jQuery 首先安装jQuery依赖 npm install jquery 然后安装jsdom npm install jsdom 引入jQuery 新版正确的依赖方式 ...