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. 

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

Sample Output

0
1
2
2
思路 : 这是一道特别经典的DFS题目 就是先找到一个@然后将与@相连的@全部变成“×” dfs结束,说明没有与@相连的点了 计一此数
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
char arr[][];
int mark[][]={};
int d[][]={{,},{,},{,-},{-,},{,},{,-},{-,},{-,-}};
void dfs(int x,int y){
mark[x][y]=;
for(int i=;i<;i++){
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&mark[dx][dy]==&&arr[dx][dy]=='@'){
arr[dx][dy]='*';
dfs(dx,dy);
}
}
}
int main()
{
while(cin>>n>>m&&m){
int ans=;
memset(mark,,sizeof(mark));
for(int i=;i<n;i++)
scanf("%s",&arr[i]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(arr[i][j]=='@')
{
dfs(i,j);
ans++;
}
}
cout<<ans<<endl;
} return ;
}

最新文章

  1. Effective java笔记(二),所有对象的通用方法
  2. 安卓智能POS终端手持机PDA应用仓库出入库,移库,盘点,销售开单系统
  3. Java - 安全的退出线程
  4. Atitit 判断判断一张图片是否包含另一张小图片
  5. webservice MaxReceivedMessageSize :已超过传入消息(65536)的最大消息大小配额
  6. SignalR入门之小试身手
  7. Hibernate(三)__核心接口和类
  8. ado.net 连接,删除,添加
  9. javascript复习总结
  10. FIO使用指南
  11. AC自动机(转)
  12. Ubuntu 14.04/14.10下安装VMware Workstation 11图文教程
  13. 用视频编辑软件打不开jpg格式的图片的解决方法
  14. 通用方法解决dedecms导航调用二级、三级栏目菜单
  15. 深究带PLL的错误复位设计
  16. JSP中 JSTL
  17. Delphi调用Android的.so文件(转)
  18. Java-大集合拆分为指定大小的小集合
  19. js原生的轮播,原理以及实践
  20. 联想功能 Jquery autocomplete.js输入框联想补全功能

热门文章

  1. TensorFlow系列专题(十三): CNN最全原理剖析(续)
  2. Springcloud zuul 实现API 网关
  3. Python python 数据类型--集
  4. iOS 设备尺寸与系统信息
  5. nginx 报 502 bad gateway 分析解决
  6. SpringBoot登录判断
  7. vue3.0+axios 跨域+封装
  8. RecyclerView 的简单使用
  9. PTA数据结构与算法题目集(中文) 7-20
  10. python爬虫两个影院的实例