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
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output

0
1
2
2 解题思路:这是一个简单的DFS问题,就是一个m*n的矩形里面有多少块封闭的@区间,用深搜的方法即可
程序代码:
#include <cstdio>
using namespace std;
int c[][]={{,},{,},{,},{-,},{-,},{-,-},{,-},{,-}};
char a[][];
int m,n,d;
void dfs(int i,int j)
{ a[i][j]='*';
for(int k=;k<;k++)
{
int x=i+c[k][];
int y=j+c[k][];
if(a[x][y]=='@'&&x>=&&x<=m&&y>=&&y<=n)
dfs(x,y);
}
return ;
} int main()
{
while(scanf("%d%d",&m,&n)==&&m&&n)
{
d=;
int i,j;
for( i=;i<m;i++)
scanf("%s",a[i]);
for( i=;i<m;i++)
for( j=;j<n;j++)
if(a[i][j]=='@')
{ d++;
dfs(i,j);
}
printf("%d\n",d);
}
return ;
}

最新文章

  1. 【SRM】649 t2
  2. CSS+Javascript
  3. C# xpath
  4. hdu 5382 GCD?LCM!
  5. svn patch用法
  6. 《Python核心编程》部分错误纠正(勘误表)(持续更新)
  7. bounds的剖析
  8. [Asp.net]SignalR实现实时日志监控
  9. Weblogic魔法堂:AdminServer.lok被锁导致启动、关闭域失败
  10. java中异步多线程超时导致的服务异常
  11. html5引用公共头尾
  12. sql语句中的 inner join 、 left join 、 right join、 full join 的区别
  13. Message,MessageQueue,Looper,Handler详解
  14. DZ真是各种强大
  15. Socket编程之聊天程序 - 模拟Fins/ModBus协议通信过程
  16. 最新发布树莓派2代Wi-Fi自动连接实战(适合初学者)
  17. Web前端面试指导(十四):如何居中一个元素(正常、绝对定位、浮动元素)?
  18. python 垃圾回收机制的思考
  19. JavaScript发布/订阅实例
  20. 建一个网站python

热门文章

  1. PHP 中xampp不能启动服务器的问题
  2. maven占位符
  3. 将MVC中的Controllers、Model和View分别放到单独的项目中
  4. 一则自用iptables例子解释
  5. 那些常用的eclipse快捷键
  6. 哈哈,CSDN又支持Windows Live Writer了
  7. photoshop cc 版本安装失败解决办法
  8. Winform使用DevExpress的WaitDialogForm画面
  9. 数据库,inner join,left join right join 的区别
  10. yii2源码学习笔记(四)