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. 【Maven】如何使用pom.xml引入自定义jar包

    这里我以这个jar包为例,aliyun-java-sdk-core-3.2.3.jar ,这是我在做手机短信服务用到的jar包 ①进入C盘下的maven仓库C:\Users\用户\.m2\reposi ...

  2. selenium中driver.close()和driver.quit()的不同点

    driver.quit()与driver.close()的不同:driver.quit(): Quit this driver, closing every associated windows;dr ...

  3. [C3] Andrew Ng - Neural Networks and Deep Learning

    About this Course If you want to break into cutting-edge AI, this course will help you do so. Deep l ...

  4. 用一个例子说明oracle临时表,创建过程,

    --创建临时表,规定好格式,是必须的,不同于sqlserver那么随意: Create Global Temporary Table record4 (   yljgdm VARCHAR2(22) n ...

  5. Springboot项目启动不了。也不打印任何日志信息。

    Springboot项目启动不了.也不打印任何日志信息. <!-- 在创建Spring Boot工程时,我们引入了spring-boot-starter,其中包含了spring-boot-sta ...

  6. CF786B Legacy 线段树优化建图

    问题描述 CF786B LG-CF786B 题解 线段树优化建图 线段树的一个区间结点代表 \([l,r]\) 区间点. 然后建立区间点的时候就在线段树上建边,有效减少点的个数,从而提高时空效率. 优 ...

  7. 修改kile工程名字(转)

    假设原来的工程文件名是first,要改成second1.在工程文件目录中,将first.uvopt和first.uvproj名字改成second.uvopt和second.uvproj.2.其他fir ...

  8. c# lock 锁

    lock语句 lock 语句获取给定对象的互斥 lock,执行语句块,然后释放 lock. 持有 lock 时,持有 lock 的线程可以再次获取并释放 lock. 阻止任何其他线程获取 lock 并 ...

  9. HTML连载17-id选择器&类选择器

    一.问题:我们前面讲了标签选择器有一个缺陷就是它不加选择的把所有相同的标签全都变成统一样式,这对于我们个性化定制产生了阻碍,因此我们便引出了id选择器,来进行特别指定进行配置样式 二.id选择器 1. ...

  10. 【shell命令】$#、$*、$n分别表示的含义

    $#.$*.$n分别表示的含义 1.[$0] 表示当前脚本的文件名: 2.[$n] 表示传递给脚本的第n个参数值(n为1~9): 3.[$*] 表示传递给脚本的所有参数(不包括脚本名称的参数): 4. ...