Oil Deposits

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

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
 
 
 
 
代码(DFS):
 #include<bits/stdc++.h>
using namespace std;
const int N=+;
char a[N][N];
int dis[][]={{,},{-,},{,},{,-},{,},{-,},{,-},{-,-}};
int n,m,num;
void DFS(int x,int y){
int xx,yy;
for(int i=;i<;i++){
xx=dis[i][]+x;
yy=dis[i][]+y;
if(xx>=&&yy>=&&xx<n&&yy<m){
if(a[xx][yy]=='@'){
a[xx][yy]='*';
DFS(xx,yy);
}
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(a,,sizeof(a));
if(n==&&m==)break;
num=;
for(int i=;i<n;i++){
for(int j=;j<m;j++)
cin>>a[i][j];
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='@'){
num++;
a[i][j]='*';
DFS(i,j);
}
}
}
printf("%d\n",num);
}
return ;
}

代码(BFS):

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=+;
char mat[N][N];
int dir[][]={{,},{-,},{,},{,-},{,},{-,},{,-},{-,-}};
int n,m,sum;
struct Node{
int x,y;
};
void BFS(int x,int y){
queue<Node>q;
Node node;
node.x=x;
node.y=y;
q.push(node);
while(!q.empty()){
Node cur=q.front();
Node next;
q.pop();
for(int i=;i<;i++){
next.x=cur.x+dir[i][];
next.y=cur.y+dir[i][];
if(mat[next.x][next.y]=='@'){
mat[next.x][next.y]='*';
q.push(next);
}
}
}
}
int main(){
while(scanf("%d%d",&n,&m)){
if(n==&m==)break;
memset(mat,,sizeof(mat));
sum=;
int cur=;
for(int i=;i<=n;i++)
scanf("%s",mat[i]+);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mat[i][j]=='@'){
sum++;
mat[i][j]='*';
BFS(i,j);
}
}
}
printf("%d\n",sum);
}
return ;
}

HDU 1241.Oil Deposits-求连通块DFS or BFS的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. HDU 1241 - Oil Deposits - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题意: 求某块平面上,连通块的数量.一个油田格子若周围八个方向也有一个油田格子,则认为两者相连通 ...

  7. HDU 1241 Oil Deposits (DFS)

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

  8. HDU 1241 Oil Deposits (DFS or BFS)

    链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...

  9. HDU 1241 Oil Deposits【DFS】

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

随机推荐

  1. LeetCode刷题感想

    断断续续用了半年的时间把LeetCode刷完了,之前复习了数据结构与算法.将刷题与复习数据结构结合起来会更有效果.总之不是为了刷题而刷题,而是为了巩固和补充一部分知识. LeetCode真的是一个很好 ...

  2. Linux命令之---find

    命令简介 find明林用于查找目录下的文件,同时也可以调用其他命令执行相应的操作 命令格式 find pathname -options [-print -exec -ok ...] find [选项 ...

  3. spark测试几个hadoop的典型例子

    1.求每年的最高温度数据格式如下: 0067011990999991950051507004888888889999999N9+00001+999999999999999999999900670119 ...

  4. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 19: ordinal not in range(128)

    解决方案: 1: 在网上找到的解决方案是: 在调用import matplotlib.pyplot as plt前 import sys sys.setdefaultencoding(“gbk”) 让 ...

  5. python 抽象类和接口类

    一.接口类 继承的两种用途: 1.继承基类的方法,并且做出自己的改变或者扩展(代码重用) 2.声明某个子类兼容于某个基类,定义一个接口类interface,接口类中定义了一些接口名(就是函数 名)  ...

  6. Struts2请求流程

    1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可 ...

  7. koa2在node6中如何运行

    koa2在node6下运行 { "babel-core": "^6.24.1", "babel-plugin-syntax-async-functio ...

  8. vue - computed

    computed 的作用主要是对原数据进行改造输出.改造输出:包括格式的编辑,大小写转换,顺序重排,添加符号……. 一.格式化输出结果: 我们先来做个读出价格的例子:我们读书的原始数据是price:1 ...

  9. 满汉全席(banquet)

    满汉全席(banquet) 题目描述 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做 ...

  10. 【02】react 之 jsx

    React与ReactDOM是react中核心对象,React为核心功能,ReactDOM提供对DOM的操作,以前的老版本中只有React没有ReactDOM,新版本中分离出ReactDOM提供两种渲 ...