Oil Deposits

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8274 Accepted Submission(s): 4860
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
 

方法一:DFS(深度优先搜索)

import java.io.*;
import java.util.*;
public class Main {
public char ch[][];
public int fx[]={1,1,1,-1,-1,-1,0,0};
public int fy[]={0,1,-1,0,1,-1,1,-1};
public int m,n;
public static void main(String[] args) {
new Main().work();
}
public void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
m=sc.nextInt();
n=sc.nextInt();
ch=new char[m][n];
if(m==0)
System.exit(0);
for(int i=0;i<m;i++){
String s=sc.next();
ch[i]=s.toCharArray();
}
int toal=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(ch[i][j]=='@'){
toal++;
dfs(i,j);
}
}
}
System.out.println(toal);
}
}
public void dfs(int x,int y){
for(int i=0;i<8;i++){
int px=x+fx[i];
int py=y+fy[i];
if(check(px,py)){
ch[px][py]='*';
dfs(px,py);
}
}
}
public boolean check(int px,int py){
if(px<0||px>m-1||py<0||py>n-1||ch[px][py]!='@')
return false;
return true;
}
}

方法二BFS(广度优先搜索)

import java.io.*;
import java.util.*;
public class Main {
public int m,n;
public char ch[][];
public Queue<Bnode> list=new LinkedList<Bnode>();
public int fx[]={1,1,1,-1,-1,-1,0,0};
public int fy[]={0,1,-1,0,1,-1,1,-1};
public static void main(String[] args) {
new Main().work();
}
public void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
list.clear();
m=sc.nextInt();
n=sc.nextInt();
if(m==0)
System.exit(0);
ch=new char[m][n];
for(int i=0;i<m;i++){
String s=sc.next();
ch[i]=s.toCharArray();
}
int total=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(ch[i][j]=='@'){
total++;
Bnode bnode=new Bnode();
bnode.x=i;
bnode.y=j;
list.add(bnode); BFS();
}
}
}
System.out.println(total);
}
}
public void BFS(){
while(!list.isEmpty()){
Bnode bnode=list.poll();
for(int i=0;i<8;i++){
int px=bnode.x+fx[i];
int py=bnode.y+fy[i];
if(check(px,py)){
ch[px][py]='*';
Bnode t=new Bnode();
t.x=px;
t.y=py;
list.add(t);
}
}
}
}
public boolean check(int px,int py){
if(px<0||px>m-1||py<0||py>n-1||ch[px][py]!='@')
return false;
return true;
}
}
class Bnode {
int x;
int y;
}

HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)的更多相关文章

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

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

  2. HDU 1241 Oil Deposits (DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. HDU 1241 Oil Deposits DFS搜索题

    题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...

  4. HDU 1241 Oil Deposits (DFS or BFS)

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

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

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

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

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

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

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

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

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

  9. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

随机推荐

  1. UVa 10635 (LIS+二分) Prince and Princess

    题目的本意是求LCS,但由于每个序列的元素各不相同,所以将A序列重新编号{1,2,,,p+1},将B序列重新编号,分别为B中的元素在A中对应出现的位置(没有的话就是0). 在样例中就是A = {1 7 ...

  2. NuGet的安装;

        下载完毕安装需要重启一下VS 然后我们去NuGet里面安装我们要的客户端 搜索  CouchbaseNetClient   引用之后,编译项目,查看到已引用的dll文件

  3. 最冤枉的关键字----sizeof

    <h4>一.常年被人误认为函数.</h4> sizeof 是关键字不是函数,其实就算不知道它是否为32 个关键字之一时,我们也可以借助编译器确定它的身份.看下面的例子: int ...

  4. 【转】Eclipse快捷键 10个最有用的快捷键----不错

    原文网址:http://www.open-open.com/bbs/view/1320934157953 1.选中你要加注释的区域,用ctrl+shift+C 会加上//注释2.先把你要注释的东西选中 ...

  5. 小结JS中的OOP(下)

    关于JS中OOP的具体实现,许多大神级的JS专家都给出了自己的方案. 一:Douglas Crockford 1.1 Douglas Crockford实现的类继承 /** * 原文地址:http:/ ...

  6. SQL0668N 由于表 "db2inst1.test" 上的原因代码 "3",所以不允许操作(解因为LOAD引起的LOAD暂挂状态锁)

    DB2解因为LOAD引起的LOAD暂挂状态锁   一般解锁命名是,SET INTEGRITY FOR temp_test IMMEDIATE CHECKED   但是load暂挂状态是解不了的,可以l ...

  7. 继承TextView简单画一个尺子

    import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; impor ...

  8. 8、NFC技术:让Android自动打开网页

    创建封装Uri的NdefRecord  public  NdefRecord  createUri(String  uriString);  public  NdefRecord  cre ...

  9. 解决:cc1.exe: sorry, unimplemented: 64-bit mode not compiled in

    在win下用Go语言的cgo时(比如下面场景)我们会用到的GCC编译器,Win下我们一般用MinGW. Golang连接Oracle数据库:win下 golang 跨平台编译 MinGW全称Minim ...

  10. 对人脑处理视觉的描述(摘《学习OpenCV(中文版)》)

    人脑将视觉信号划分入很多个通道,将各种不同的信息输入你的大脑.你的大脑有一个关注系统,会根据任务识别出图像的重要部分,并做重点分析,而其他部分则分析得较少 .在人类视觉流中存在大量的反馈,但是目前我们 ...