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

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 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

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
  5.  
  6. 本题是经典的DFS题,利用DFS求连通块的数量,DFS利用的是递归实现
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. const int maxn = 100+5;
  5.  
  6. char pic[maxn][maxn]; //图表
  7. int m, n, idx[maxn][maxn]; //连通分量编号矩阵
  8.  
  9. //用DFS的方法为连通分量矩阵编号
  10. void DFS(int r, int c, int id)
  11. {
  12. if(r<0||r>m||c<0||c>=n)return; //overload
  13. if(idx[r][c]>0||pic[r][c]!='@')return; //no '@' or 已经访问
  14. idx[r][c]=id; //连通分量编号
  15. for(int dr=-1; dr<=1;dr++)
  16. for(int dc=-1;dc<=1;dc++)
  17. if(dr!=0||dc!=0)DFS(r+dr, c+dc, id);
  18. }
  19.  
  20. int main()
  21. {
  22. while(cin>>m>>n&&m!=0)
  23. {
  24. for(int i=0;i<m;i++)
  25. for(int j=0;j<n;j++)
  26. cin>>pic[i][j];
  27. for(int i=0;i<m;i++)
  28. for(int j=0;j<n;j++)
  29. idx[i][j]=0;
  30. int cnt=0;
  31. for(int i=0;i<m;i++)
  32. for(int j=0;j<n;j++)
  33. if(idx[i][j]==0&&pic[i][j]=='@')DFS(i,j,++cnt);
  34. cout<<cnt<<endl;
  35. }
  36. return 0;
  37. }

 

可以参考floodfill算法

https://en.wikipedia.org/wiki/Flood_fill 

  1.  

[POJ] 1562 Oil Deposits (DFS)的更多相关文章

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

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

  2. POJ 1562 Oil Deposits (并查集 OR DFS求联通块)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14628   Accepted: 7972 Des ...

  3. (简单) POJ 1562 Oil Deposits,BFS。

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

  4. POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS

    现在,又可以和她没心没肺的开着玩笑,感觉真好. 思念,是一种后知后觉的痛. 她说,今后做好朋友吧,说这句话的时候都没感觉.. 我想我该恨我自己,肆无忌惮的把她带进我的梦,当成了梦的主角. 梦醒之后总是 ...

  5. POJ 1562 Oil Deposits

    转载请注明出处:http://blog.csdn.net/a1dark 大规模的图论切题之旅正式开始了.由于今天停了一天的电.所以晚上才开始切题.直到昨晚才把图论大概看了一遍.虽然网络流部分还是不怎么 ...

  6. poj 1562 Oil Deposits (广搜,简单)

    题目 简单的题目,只是测试案例的输入后面可能有空格,所以要注意一下输入方式. #define _CRT_SECURE_NO_WARNINGS //题目的案例输入n,m后面有些貌似有空格... #inc ...

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

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

  8. Oil Deposits(dfs)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

随机推荐

  1. 一步步优化JVM六:优化吞吐量

    如果你已经进行完了前面的步骤了,那么你应该知道这是最后一步了.在这一步里面,你需要测试应用的吞吐量和为了更高的吞吐量而优化JVM.    这一步的输入就是应用的吞吐量性能要求.应用的吞吐量是在应用层面 ...

  2. COCOS2D-JS入门-官网template源码解析

    首先介绍几个概念: 导演: 导演 (Director)是Cocos2d-JS引擎抽象的一个对象,Director是整个Cocos2d-JS引擎的核心,是整个游戏的导航仪,游戏中的一些常用操作就是由Di ...

  3. [读书笔记]python3.5实现socket通讯(UDP)

    UDP连接: 无连接,从一个端向另一端发送独立的数据分组 使用UDP连接的客户-服务器程序: UDPServer.py import socket serverPort = 50009 serverS ...

  4. 【ARM】S5PV210芯片中的BL0的作用

    S5PV210芯片中的BL0的作用:(1)关闭看门狗:(2)清除指令寄存器:(3)初始化栈区域:(4)初始化堆区域:(5)初始化块设备复制功能:(6)初始化PLL和设置系统时钟:(7)拷贝BL1到片内 ...

  5. 修改redis端口号

    为redis分配一个8888端口,操作步骤如下:1.$REDIS_HOME/redis.conf重新复制一份,重命名为redis8888.conf.2.打开redis8888.conf配置文件,找到p ...

  6. swift3 循环滚动视图 自适应横竖屏 reload数据源

    一句话创建banner图,可时时刷新 用到了 SnapKit.SDWebImage两个第三方库 实现步骤------------------------------------------------ ...

  7. css-div下内容垂直居中

    1.多行行文字在固定高度的div中垂直居中,只兼容高级浏览器和移动端 .detail { width: 395px; height: 289px; display: -webkit-box; -web ...

  8. 递归——CPS(二)

    给出一个计算树深度的函数: function treeDepth(curtree) { if(curtree == null) return 0; else { var leftDepth = tre ...

  9. UNIX基础--磁盘组织

    磁盘组织 FreeBSD 查找文件的最小单位是文件名. 而文件名区分大小写,不凭文件扩展名去识别这个文件是 程序. 文档, 或是其他格式的数据. 在文件系统里目录和文件的作用是存储数据. 每一个文件系 ...

  10. Scala内部类

    注意:Java内部类从属于外部类,而Scala内部类从属于对象(外部类的实例本身).