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. acdream 1682 吃不完的糖果(环形最大子段和)

    Problem Description 娜娜好不容易才在你的帮助下"跳"过了这个湖,果然车到山前必有路,大战之后必有回复,大难不死,必有后福!现在在娜娜面前的就是好多好多的糖果还有 ...

  2. Dom对象的方法应用一getElementById技巧、getElementsByName() IE,firefox兼容

    在document对象中有以下三个方法,对于程序员来说,真可谓无人不知,无人不晓,他们分别是: 1.getElementById()                  返回对拥有指定 id 的第一个对 ...

  3. python练习程序(c100经典例21)

    题目: 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早上想再吃时,见只 ...

  4. 【C#学习笔记】浏览目录得到路径

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Maven加依赖包

    对于初学maven的人来说刚开始会有个困惑,那就是怎么知道依赖的jar的groupId和atrifactId是什么, 比如要依赖mybatis,会在pom.xml中配置如下: <dependen ...

  6. openssl安装

    www.openssl.orgconfigure the environment:<pre lang="bash" escaped="true">t ...

  7. order by多个字段对索引的影响

    某前台sql语句,简化后如下SELECT products_name,products_viewed FROM `products_description` ORDER BY products_vie ...

  8. mysql 表日常变化前几

    mysql 表日常变化前几use performance_schema create table test.snap1 as SELECT OBJECT_SCHEMA, OBJECT_NAME, CO ...

  9. OpenGL学习之路(一)

    1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...

  10. Dev GridView 获取选中分组下的所有数据行 z

    现在要在DevExpress 的GridView 中实现这样一个功能.就是判断当前的选中行是否是分组行,如果是的话就要获取该分组下的所有数据信息. 如下图(当选中红框中的分组行事.程序要获取该分组下的 ...