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. poj 3045 叠罗汉问题 贪心算法

    题意:将n头牛叠起来,每头牛的力气 s体重 w  倒下的风险是身上的牛的体重的和减去s 求最稳的罗汉倒下去风险的最大值 思路: 将s+w最大的放在下面,从上往下看 解决问题的代码: #include& ...

  2. A brief look at the Objects in JavaScript

    Objects  An object is a self-contained collection of data. This data comes in to forms:  properties ...

  3. laravel5.2总结--门面(facades)

    Facades 为应用程序的服务容器中可用的类提供了一个「静态」接口.   Laravel 本身附带许多的 facades,甚至你可能在不知情的状况下已经在使用他们!   xpower的静态接口(门面 ...

  4. IOS开发---菜鸟学习之路--(十六)-将Image转换为Base64

    我们直接在.m文件的引用头文件部分 和 @interface   AddPictureViewController () 之间  加入 增加部分的代码 然后就可以使用图片转Base64了 #impor ...

  5. 聊聊、RabbitMQ 第一篇

    (一)windows 下安装配置 开源的消息中间件有很多,各有各的优缺点,适合自己项目的才是最好的.首先下载 rabbitMQ 安装版本,因为 rabbitMQ 底层语言是 erlang,所以首先要先 ...

  6. linux常用命令(复制)

    显示目录和文件的命令    Ls:用于查看所有文件夹的命令. Dir:用于显示指定文件夹和目录的命令   Tree: 以树状图列出目录内容 Du:显示目录或文件大小 修改目录,文件权限和属主及数组命令 ...

  7. MySql数据库 - 4.可视化操作数据库

    创建表 对表中数据进行  增.删.改.查 查 右键刚刚创建的表 - 选择查看前 1000 条数据 增.改 表格必须有主键才能添加数据,主键是不能重复的 1. 右键表 - 查看前 1000 条数据 2. ...

  8. diea

    http://name.vip.int ellig.top/name

  9. JavaWeb开发小结

    JavaWeb开发是B/S开发的一种,其他语言也可以实现,就前台而言,一直都是HTML.CSS.JavaScript 后台语言可以是Perl.Python.PHP.Java等等 Java机缘巧合在We ...

  10. Arcengine 基本操作(待更新)

    /// <summary> /// 删除fieldName属性值为1的弧段 /// </summary> /// <param name="fieldName& ...