GeoSurvComp地质调查公司负责探测地下石油储藏。 GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块。他们通过专业设备,来分析每个小块中是否蕴藏石油。如果这些蕴藏石油的小方格相邻,那么他们被认为是同一油藏的一部分。在这块矩形区域,可能有很多油藏。你的任务是确定有多少不同的油藏。

Input

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

Output

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

Sample Input

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

Sample Output

0
1
2
2

思路

最基本的dfs计算连通分量,注意检查边界及是否为油田即可。

代码

import java.util.Scanner;
public class HDU1241 {private static int row, col;private static boolean[][] data;private static boolean[][] marked;private static void dfs(int i, int j){if(!data[i][j]) return;marked[i][j] = true;if(i + 1 < row && !marked[i + 1][j]) dfs(i + 1, j);if(i - 1 >= 0 && !marked[i - 1][j]) dfs(i - 1, j);if(j + 1 < col && !marked[i][j + 1]) dfs(i,j + 1);if(j - 1 >= 0 && !marked[i][j - 1]) dfs(i,j - 1);if(i + 1 < row && j + 1 < col && !marked[i + 1][j + 1]) dfs(i + 1, j + 1);if(i + 1 < row && j - 1 >= 0 && !marked[i + 1][j - 1]) dfs(i + 1, j - 1);if(i - 1 >= 0 && j + 1 < col && !marked[i - 1][j + 1]) dfs(i - 1, j + 1);if(i - 1 >= 0 && j - 1 >= 0 && !marked[i - 1][j - 1]) dfs(i - 1, j - 1);}public static void main(String[] args){Scanner in = new Scanner(System.in);while (true){row = in.nextInt();col = in.nextInt();if(row == 0 && col == 0) break;int count = 0;in.nextLine();data = new boolean[row][col];marked = new boolean[row][col];for(int i = 0; i < row; i++){char[] c = in.nextLine().toCharArray();for(int j = 0; j < col; j++){data[i][j] = c[j] == '@';}}for(int i = 0; i < row; i++){for(int j = 0; j < col; j++){if(!marked[i][j] && data[i][j]){dfs(i, j);count++;}}}System.out.println(count);}}
}

【DFS】【水】HDU1241 Oil Deposits 油田资源相关推荐

  1. HDU-1241 Oil Deposits (DFS)

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

  2. NUC1397 Oil Deposits【DFS】

    Oil Deposits 时间限制: 1000ms 内存限制: 65535KB 问题描述 The GeoSurvComp geologic survey company is responsible ...

  3. UVa572 Oil Deposits DFS求连通块

    技巧:遍历8个方向 for(int dr = -1; dr <= 1; dr++)for(int dc = -1; dc <= 1; dc++)if(dr != 0 || dc != 0) ...

  4. 【POJ - 1562】Oil Deposits (dfs搜索,连通块问题)

    题干: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...

  5. 【HDU - 1241】Oil Deposits (连通块问题 属于求大海中的岛屿个数 类似问题)

    题干: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...

  6. L - Oil Deposits

    题目描述 The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...

  7. 百-寒-进-16-油田Oil Deposits

    Oil Deposits 题目描述 GeoSurvComp地质调查公司负择探测地下石油矿床.该公司一次处理一个大的矩形区域,并创建一个网格,将土地划分为许多正方形地块.然后,它分别分析每个地块,使用传 ...

  8. L - Oil Deposits HDU - 1241

    L - Oil Deposits HDU - 1241 注意写check的时候要先判x,y是不是越界了,不然直接用数组可能RE, 也可能直接WA了 #include<cstdio> #in ...

  9. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

最新文章

  1. WCF 第十三章 可编程站点 所有都与URI相关
  2. 出色性能服务器,浪潮服务器:演绎出色传输与存储性能
  3. C#开发Android应用的必备——Mono for Android V1.0 发布
  4. 鸿蒙硬件HI3861-4G透传模块-MODBUS
  5. 一个核心系统 3 万多行代码的重构之旅
  6. jQuery Validate 表单验证插件----在class属性中添加校验规则进行简单的校验
  7. 小米5s升级Android8,小米5s、小米5s Plus升级8.0提前,好消息!
  8. 2022年计算机二级C语言程序设计模拟练习题及答案
  9. wap2app是什么
  10. 专业课课本复习(数电,通原,数信,信号与系统,数据结构)
  11. IT零起点转FICO学习路线(转)
  12. 设置老版版谷歌浏览器自动启用flash
  13. python : pandas 中多重索引multiindex与多个标的dataframe
  14. Java并发(四)BlockingQueue的使用
  15. 2021年中国综艺赞助情况回顾及未来发展趋势:品牌更乐于与成熟的综N代合作,未来合作方式更多元化[图]
  16. 燃气热水器打不着火水压低的解决方法(zt)
  17. Vivado中ILA(集成逻辑分析仪)的使用
  18. 【python】 程序设计基础
  19. 使用jmeter进行接口压力性能测试
  20. cpu压力 测试软件,破坏测试工具——CPU压力

热门文章

  1. 使用E-mailCrack破解邮箱密码案例
  2. 【ASP.NET】GRIDVIEW控件的一般使用技巧
  3. 基于深度残差收缩网络的故障诊断 Deep Residual Shrinkage Networks for Fault Diagnosis
  4. 信号归一化功率_利用电机的电信号来检测轴承性能退化
  5. 电脑提示丢失d3dx11_43.dll如何修复-一键自动修复
  6. 支付功能测试用例设计要点
  7. linux sftp 设置根目录,FTP,SFTP服务器登录权限和根目录的设置
  8. 845GL也是可以玩魔兽争霸的,原来!
  9. 软考(软件设计师知识点) --多媒体基础
  10. WPF WrapPanel