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<iostream>
  3. using namespace std;
  4. int n,m;
  5. char map[][];
  6. int rx[]={,,,-,,,-,-};
  7. int ry[]={,-,,,,-,,-};
  8. void del(int i,int j)
  9. {
  10. map[i][j]='*';
  11. int x,y,t;
  12. for(t=;t<;t++)
  13. {
  14. x=i+rx[t];
  15. y=j+ry[t];
  16. if(x>=&&x<n&&y>=&&y<m&&map[x][y]=='@')
  17. del(x,y);
  18. }
  19. }
  20. int main()
  21. {
  22. while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
  23. {
  24. int i,j,num=;
  25. for(i=;i<n;i++)
  26. for(j=;j<m;j++)
  27. cin>>map[i][j];
  28. for(i=;i<n;i++)
  29. for(j=;j<m;j++)
  30. if(map[i][j]=='@')
  31. {
  32. num++;
  33. del(i,j);
  34. }
  35. printf("%d\n",num);
  36. }
  37. return ;
  38. }

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

  1. hdu 1241Oil Deposits(dfs模板)

    题目链接—— http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,‘*’表示空地,‘@’表示油井.问在这个矩阵中有多少组油井区? ...

  2. hdu 1241Oil Deposits(BFS)

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

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

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

  4. HDOJ(HDU).1015 Safecracker (DFS)

    HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...

  5. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  6. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

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

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

  8. HDU 1241 Oil Deposits (DFS/BFS)

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

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

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

随机推荐

  1. Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)的解决方法

    在连接数据库时,报这个错误,是/var/lib/mysql/ 目录下没有mysql.sock文件,在服务器搜索myslq.sock文件,我的是在/tmp/mysql.sock 解决方法是加一个软链: ...

  2. (十一)if...else&for循环&while循环

    ----------------------------------if else------------------------------1.最基本的if语句:if name =="Al ...

  3. KB2533623 下载

    服务器上要部署.NET Core 的环境, 先要在服务器上安装Core SDK.直达连接 下载安装一切顺利: 下面开始检验是否正确安装了↓ 运行→cmd→dotnet 结果报错↓ Failed to ...

  4. JavaScript进阶(三)

    现在来说说判断语句(if)if语句是基于条件成立才执行相应代码时使用的语句.语法:if(条件){条件成立时执行代码}注意:if小写,大写字母(IF)会出错!假设你应聘web前端技术开发岗位,如果你会h ...

  5. delphi用TAdoStoredProc调用存储过程,兼容sql2005、2008、2014的远程事务问题

    delphi7写的程序,在sql2000里没问题,调用sql2008.2014里的存储过程时,如果存储过程里操作了大量数据,很容易会莫名其妙的自己撤销掉,但是程序还识别不到,认为还在正常执行.今天尝试 ...

  6. 开源的许可证GPL、LGPL、BSD、Apache 2.0的通俗解释

    软件开发者要开源软件,不单单是开放源代码就可以了,选择一种许可证很重要,一个许可证之于软件就相当于价值观之于普通人,代表了这个软件的基本品性.一个错误的许可证选择可能会直接导致整个项目的失败. 各种开 ...

  7. RubyMine 2016.1 下载 附注册激活码 破解版方法

    注册破解方法: 在要求输入注册的界面选择激活码,然后粘贴以下注册码: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIj ...

  8. QT学习笔记—1

    1.模态和非模态的区别:非模态可以同时操作两个窗口,模态的只能在顶层窗口关闭之后才能使用其他窗口 //同时显示出widget和dialog窗口,非模态     QDialog *dialog = ne ...

  9. <hdu - 1280> 前M大的数 (注意其中的细节)

    这是杭电hdu上的链接http://acm.hdu.edu.cn/showproblem.php?pid=1280  Problem Description: 还记得Gardon给小希布置的那个作业么 ...

  10. unity3D中重要函数

    Update 当MonoBehaviour启用时,其Update在每一帧被调用. LateUpdate 当Behaviour启用时,其 LateUpdate在每一帧被调用. FixedUpdate 当 ...