背景描述

ztw同志负责探测地下石油储藏.ztw现在在一块矩形区域探测石油.他通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石油的小方格相邻(横向相邻,纵向相邻,还有对角相邻),那么它们被认为是同一油藏的一部分.在这块矩形区域,可能有很多油藏.你的任务是确定有多少不同的油藏.

输入

输入可能有多个矩形区域(即可能有多组测试)。每个矩形区域的起始行包含m和n,表示行和列的数量,1<=n,m<=100,如果m=0表示输入的结束,接下来是n行,每行m个字符.每个字符对应一个小方格,并且要么是‘*’、代表没有油,要么是‘@’,表示有油.(注意多组数据,必要的数组要及时清空~)

输出

对于每一个矩形区域,输出油藏的数量.两个小方格是相邻的,当且仅当他们水平或者垂直或者对角线相邻(即8个方向)。

示例输入

 
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5 
****@
*@@*@
*@**@
@@@*@
@@**@
0 0 
 

示例输出


0
1
2
2
 
代码:

import java.util.Scanner;

public class Main{
static int n,m,cnt;
static final int N=105;
static char map[][]=new char[N][N];
static int dx[]={0,0,1,-1, 1,-1,1,-1};
static int dy[]={1,-1,0,0,1,-1,-1,1};
static void dfs(int x,int y){
if(map[x][y]=='@'){
map[x][y]='*';
}
for(int i=0;i<8;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(xx<0 ||yy<0 ||xx>=n ||yy>=m) continue;
if(map[xx][yy]=='*') continue;
dfs(xx,yy);
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
n=scan.nextInt();
m=scan.nextInt();
if(n==0 && m==0) break;
for(int i=0;i<n;i++){
map[i]=scan.next().toCharArray();
}
cnt=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='@'){
dfs(i,j);
cnt++;
}
System.out.println(cnt);
}
}
}

HDU1241 Oil Deposits(dfs+连通块问题)的更多相关文章

  1. HDU1241 Oil Deposits —— DFS求连通块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  2. HDU-1241 Oil Deposits (DFS)

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

  3. Hdu1241 Oil Deposits (DFS)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

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

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

  5. UVa572 Oil Deposits DFS求连通块

      技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...

  6. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

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

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

  8. UVa 572 Oil Deposits(DFS)

     Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil ...

  9. [POJ] 1562 Oil Deposits (DFS)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16655   Accepted: 8917 Des ...

  10. HDU1241 Oil Deposits 2016-07-24 13:38 66人阅读 评论(0) 收藏

    Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible for detectin ...

随机推荐

  1. SpringBoot缓存 --(二)Redis单机缓存

    pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. css基本概念与css核心语法介绍

    css基本概念 css是什么?不需要了解太多文字类介绍,记住css是层叠样式表,HTML是页面结构,css负责页面样式,javascrt负责静态页面的交互.CSS 能够对网页中元素位置的排版进行像素级 ...

  3. day19 几个模块的学习

    # 模块本质上就是一个 .py 文件# 数据类型# 列表.元组# 字典# 集合.frozenset# 字符串# 堆栈:特点:先进后出# 队列:先进先出 FIFO # from collections ...

  4. cobaltstrike使用笔记2

    0x01 cs服务端绕过流量检测 定义C2的通信格式,修改CS默认的流量特征 编写Profiles: 开源Profiles:https://github.com/rsmudge/Malleable-C ...

  5. 【EasyUI总结】EasyUI开发中遇到的坑

    普遍: 1.easyui在书写键值对的时候要注意是否要加引号,在需要加引号的地方不加则无法渲染: datagrid数据网格: 1.datagrid默认请求方式是post,如果要使用分页功能pagina ...

  6. Elasticsearch启动、停止脚本

    注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/es-shell ...

  7. 复习mongoose的基本使用

    mongodb参考 mongoose官网 mongoose用起来更便捷,更方便些

  8. BIOS和DOS中断大全

    DOS中断: 1.字符功能调用类(Character-Oriented Function)01H.07H和08H —从标准输入设备输入字符02H —字符输出03H —辅助设备的输入04H —辅助设备的 ...

  9. [PAT] A1022 Digital Library

    [题目大意] 给出几本书的信息,包括编号,名字,出版社,作者,出版年份,关键字:然后给出几个请求,分别按照1->名字,2->出版社等对应信息查询符合要求的书的编号. [思路] 模拟. [坑 ...

  10. nginx 配置 强制访问https

    使用nginx的301状态码 server { listen ;     if ($scheme = 'http') {    return 301 https://$server_name$requ ...