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. C++多态实例

    #include <iostream> #include <string> using namespace std; //class 实现 class Employee { s ...

  2. 4 Values whose Sum is 0 POJ - 2785

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 29243   Accep ...

  3. Gym - 101981D Country Meow(模拟退火)

    题意 三维空间有\(n\)个点,找到另外一个点,离所有点的最大距离最小.求这个距离. 题解 \(1\).最小球覆盖,要找的点为球心. \(2\).模拟退火. 还是补一下模拟退火的介绍吧. 模拟退火有一 ...

  4. Sublime Text配置python以及快捷键总结

    1.打开Tools > Build System > New Build System.. 2.点击New Build System后,会生成一个空配置文件,在这个配置文件内覆盖配置信息, ...

  5. 《鸟哥的Linux私房菜》学习笔记(0)——磁盘与文件系统管理

    一.Linux的登陆流程 login: 用户名:每个用户名都有一个用户ID(用户标识符),计算机处理的就是用户ID(数字)而不是用户名(字符),. 认证机制:Authentication,比如密码或者 ...

  6. Java面向对象---类与对象的关系

    类是对某一类事物的描述,是抽象的.概念上的意义.对象是实际存在的该类事物的每一个个体,也被称为实例. 创建对象:包括声明对象和实例化对象 Person person;//声明 person = new ...

  7. laravel5.2总结--响应

      1 基本响应 1.1 返回一个字符串,指定的字符串会被框架自动转换成 HTTP 响应. Route::get('/', function () { return 'Hello World'; }) ...

  8. Assigning Logon Hours

    Assigning Logon Hours Updated: March 28, 2003 Applies To: Windows Server 2003, Windows Server 2003 R ...

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

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

  10. Python基础-week02 Python的常用数据类型

    一.模块初识 import导入Py自带模块例如os,sys等及其自己编写的Py文件,导入到其他文件中,默认查找当前目录.如果不在同一目录,会报错,将该自定义py文件模块放到site-packages目 ...