Oil Deposits

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 14

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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 1
  2. *
  3. 3 5
  4. *@*@*
  5. **@**
  6. *@*@*
  7. 1 8
  8. @@****@*
  9. 5 5
  10. ****@
  11. *@@*@
  12. *@**@
  13. @@@*@
  14. @@**@
  15. 0 0

Sample Output

  1. 0
  2. 1
  3. 2
  4. 2

Source

Mid-Central USA 1997
  1. #include<stdio.h>
  2. #include<string.h>
  3. char f[][]={,,,,,,-,,-,,-,-,,-,,-};//定义方向数组,8个方向,多了个折线方向。
  4. int n,m;
  5. char map[][];
  6. void dfs(int x,int y)
  7. {
  8. int x1,y1,i;
  9. for(i=;i<;i++)
  10. {
  11. x1=x+f[i][];
  12. y1=y+f[i][];
  13. if(x1>=n||y1>=m||x1<||y1<||map[x1][y1]!='@')
  14. continue;
  15. map[x1][y1]='*';//把访问过的@变成*,下次就不会再访问。
  16. dfs(x1,y1);
  17. }
  18. }
  19. int main()
  20. {
  21. int i,j,s;
  22. while(~scanf("%d%d",&n,&m)&&n!=&&m!=)
  23. {
  24. s=;
  25. for(i=;i<n;i++)
  26. scanf("%s",map[i]);
  27. for(i=;i<n;i++)
  28. for(j=;j<m;j++)
  29. if(map[i][j]=='@')
  30. {
  31. map[i][j]='*';
  32. s++;
  33. dfs(i,j);
  34. }
  35. printf("%d\n",s);
  36. }
  37. return ;
  38. }
 

HDU-1241 Oil Deposits (DFS)的更多相关文章

  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) Total ...

  3. HDU 1241 Oil Deposits (DFS/BFS)

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

  4. HDU 1241 Oil Deposits (DFS or BFS)

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

  5. HDU 1241 Oil Deposits DFS搜索题

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

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

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

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

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

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

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

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

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

  10. hdu 1241:Oil Deposits(DFS)

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

随机推荐

  1. iOS-开发日志-UIimageView

      UIImageView属性   1.Image 设置图片,默认显示 UIImageView *_imageView = [[UIImageView alloc]init]; _imageView. ...

  2. 2014年12月23日00:42:54——PS4

    http://tieba.baidu.com/p/3415598015?see_lz=1&pn=1 http://tieba.baidu.com/p/3188981817 http://tie ...

  3. 09_Mybatis开发Dao方法——mapper代理开发规范

    一.开发规范 需要编写mapper.xml映射文件(本项目为userMapper.xml,类似于前面的user.xml). 编写mapper接口需要遵循一些开发规范,这样MyBatis可以自动生成ma ...

  4. Bzoj 3831 [Poi2014]Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...

  5. 管理员把我的admin权限去掉了,那么如何获得jdk zip安装呢?这篇可以帮你。

    JDK is not available as a portable zip unfortunately. However, you can: Create working JDK directory ...

  6. 代码笔记-触摸事件插件hammer.js使用

    如果要使用jquery,则需要下载jquery.hammer.min.js版本 新建一个hammer对象生成的对象是dom对象,不能直接使用jqeury 的  $(this)方法,需要先将其转成jqu ...

  7. express的基本配置项

    express自动生成的app.js中有一段代码用app.set和app.use对express进行配置,但这些配置都是什么意思,以及都能做哪些配置并没有展开.这一节就专门来讲express的配置.上 ...

  8. chgrp命令

    chgrp命令用于变更文件或目录的所属群组. 在UNIX系统家族里,文件或目录权限的掌控以拥有者及所属群组来管理.您可以使用chgrp指令去变更文件与目录的所属群组,设置方式采用群组名称或群组识别码皆 ...

  9. Eyeshot Ultimate 学习笔记(2)

    导入模型 一般情况下,我们自己搭建模型的功力还不够,大多都是在3Dmax中做好模型,导出成模型文件,然后再导入Eyeshot视图中.导入的代码包括: OpenFileDialog openFileDi ...

  10. mysql开启日志记录慢查询

    1.查看mysql配置 2.利用set (variables)命令设置变量 set global log_slow_queries = ON;/*(必须带上global)*/ set global s ...