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. HDU 2064 (递推) 汉诺塔III

    将柱子从左到右依次编号为A.B.C 设将n个盘子从一端移动到另一端的最少步数为f(n) 则f(n)和f(n-1)的递推关系为:f(n) = 3 × f(n-1) + 2 初始状态A柱子上面有n个盘子, ...

  2. kafka_2.9.2-0.8.1.1分布式集群搭建代码开发实例

    准备3台虚拟机, 系统是RHEL64服务版. 1) 每台机器配置如下:$ cat /etc/hosts    # zookeeper hostnames:       192.168.8.182    ...

  3. UITableView中的(NSIndexPath *)indexPath

    indexPath 用来指示当前单元格,它的row方法可以获得这个单元格的行号,section方法可以获得这个单元格所处的区域号

  4. http server 下载地址

    windows 64为位:https://www.apachelounge.com/download/

  5. UVA 10061 How many zero's and how many digits ? (m进制,阶乘位数,阶乘后缀0)

    题意: 给出两个数字a和b,求a的阶乘转换成b进制后,输出 (1)后缀中有多少个连续的0? (2)数a的b进制表示法中有多少位? 思路:逐个问题解决. 设a!=k.  k暂时不用直接转成b进制. (1 ...

  6. gridview自定义表头

    gridview为我们提供了丰富的接口,用于满足自定义需求. 通常asp:gridview会根据绑定的列Columns自动生成表头,展现在前台元素. 序号 类别 有时候需要复杂一些的表头. 序号 类别 ...

  7. 【转】使用oschina的git服务器

    原文网址:http://blog.csdn.net/zengraoli/article/details/24975551 1.概要 其实oschina的git服务器与github的差不多,不过既然是中 ...

  8. 为Linux版本Oracle 11gR2配置HugePage

    HugePage是Oracle在Linux版本下一种性能优化手段.对于共享内存区域(SGA)的数据库系统,Oracle通常都推荐在操作系统层面配置上HugePage,为Oracle实例准备更大的可用共 ...

  9. node前端自动化

    一.前端自动化-项目构建 我们平时写代码,喜欢建一个project,然后里面是css.js.images文件,以及index.html,而node可以自动化构建好项目,如下: /** * Create ...

  10. 0bjective-c 之 NSString 使用详解

    查找资料的时候发现不错的文章,自己翻译之后分享给大家! 一个基本的该类型字符串例子: @"This is a constant character string object"; ...