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. [Noip2016]换教室(期望+DP)

    Description 题目链接:Luogu Solution 这题结合了DP和概率与期望,其实只要稍微知道什么是期望就可以了, 状态的构造很关键,\(F[i][j][0/1]\)表示已经到第\(i\ ...

  2. Ubuntu下Python无法识别中文

    在NLP的相关任务中,应用python处理中文是很常见的.在这个过程中,由于编码方式的不一致,可能会出现以下两种错误: 1)SyntaxError:  Non-ASCII character in f ...

  3. (WPF&Silverlight)silverlight自定义控件

    2个半小时弄懂了自定义控件是怎么回事儿. 在silverlight中创建一个UserControl,把上面sliderbar的外观和功能都封装在里面. 以自定义控件mapslider控件为例: 1.首 ...

  4. 3 View - Response对象

    1. HttpResponse对象:返回数据 from django.http import HttpResponse 在django.http模块中定义了HttpResponse对象的API Htt ...

  5. loj2052 「HNOI2016」矿区

    学习一发平面图的姿势--ref #include <algorithm> #include <iostream> #include <cstdio> #includ ...

  6. 33、Android Support兼容包详解(转载)

    原文转自:微信分享 2015-03-31 22:11 背景 来自于知乎上邀请回答的一个问题Android中AppCompat和Holo的一个问题?, 看来很多人还是对这些兼容包搞不清楚,那么干脆写篇博 ...

  7. leetcode 【 Remove Element 】python 实现

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  8. vue-devtools安装

    https://www.cnblogs.com/yuqing6/p/7440549.html

  9. camelot工具进行pdf表格解析重建

    camelot内置生成html文件的方法,但表格数据转化成pandas.dataframe的过程中,丢失了跨行跨列的结构信息,故生成html的表格无跨行跨列结构. 于是我在输出部分选择直接手写html ...

  10. SQL2008非域环境直接使用WINDOWS登录的镜像设置

    1.检查主库是否为完全备份 2.将数据库备份出来还原到同步库上(完整备份和事务日志分两次备份到同一个备份文件中,然后拷贝到同步机上) 3.用证书太麻烦了,我们直接用两个windows认真的账户 不分主 ...