转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82706189作者:Mercury_Lc

题目链接

题解:每个点(为被修改,是#)进行一次dfs,每次dfs到的点,也就是八个方向都将  '#'  修改成  '*',下次dfs就不用再搜索这一点了,因为已经确定这个点和前面的点是一个部分,这样遍历一遍图,如果可以dfs(i,j),ans++,最后ans就是答案了。当然也可以用bfs思路来想(点一下我QWQ)。

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e3;
int n,m;
char gra[maxn][maxn];
int dx[] = {1,1,1,-1,-1,-1,0,0};
int dy[] = {0,1,-1,1,0,-1,1,-1};
void dfs(int i, int j)
{
gra[i][j] = '*';
for(int k = 0; k < 8; k ++)
{
int tx = dx[k] + i;
int ty = dy[k] + j;
if(tx >= 0 && tx < n && ty >= 0 && ty < m && gra[tx][ty] == '@')
{
dfs(tx,ty);
}
}
}
int main()
{
int ans = 0;
while(~scanf("%d %d",&n,&m)&&n&&m)
{
ans = 0;
for(int i = 0; i < n; i ++)
{
getchar();
scanf("%s",gra[i]);
}
for(int i =0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(gra[i][j] == '@')
{
dfs(i,j);
ans ++;
}
}
}
printf("%d\n",ans);
}
return 0;
}

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

Sample Output

0
1
2
2

最新文章

  1. Java中普通代码块,构造代码块,静态代码块执行顺序
  2. 转: Red Hat/Fedora Linux 上使用 yum 安装 python pip 模块
  3. 项目移植将eclipse里面的项目移植到intellij idea里面
  4. web2.0最全的国外API应用集合
  5. iOS 开发-单元测试
  6. GUI按键绑定到键盘和打印组件
  7. R - 一只小蜜蜂...(第二季水)
  8. 在OpenWrt上编写自己的硬件操作程序
  9. 【翻译】CSS Animations VS the Web Animations API:案例学习
  10. BZOJ_2788_[Poi2012]Festival_差分约束+tarjan+floyed
  11. SkylineGlobe7.0.1版本 主页面如何和Popup里面的嵌入页面相互传值
  12. Codeforces Round #521 (Div. 3)
  13. C#读取Excel文件的简单方法
  14. ios-静态库,动态库,framework浅析(一)
  15. Java中JSONObject相关操作
  16. 购物商城学习--第三讲(tomcat插件启动web工程)
  17. pandas时间序列分析和处理Timeseries
  18. 新手向-同步关键字synchronized对this、class、object、方法的区别
  19. opencv:级联分类器训练(cascade classifier training)(两个分类器的区别)
  20. 六:SpringCloud-Config

热门文章

  1. MySQL优化 - 性能分析与查询优化(转)
  2. 华为精益敏捷专家:DevOps转型中的那些坑
  3. Codeforces 1247E. Rock Is Push
  4. .js文件中文乱码解决办法
  5. C语言快速判断素数——不超时
  6. element消息提示封装
  7. CentOS7中使用yum安装Nginx的详细步骤
  8. 4.SpringMVC 配置式开发-处理器映射器
  9. Delphi 特性限定符
  10. spring 通用mapper的一些注解