Oil Deposits

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64

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

【题目来源】

Mid-Central USA 1997

【题目大意】

在一个郊区的空地里,分散着很多油田,要你求油田的数量。

【题目分析】

就是一个简单的图搜索,不断标记,不断统计。

#include<iostream>
#include<cstdio>
#define MAX 150
using namespace std;
char Map[MAX][MAX];
int n,m;
int cnt;
int dir[][]={-,, -,, ,, ,, ,, ,-, ,-, -,-}; void dfs(int x,int y)
{
Map[x][y]='*';
int xx;
int yy;
for(int i=;i<;i++)
{
xx=x+dir[i][];
yy=y+dir[i][];
if(Map[xx][yy]=='@')
dfs(xx,yy);
}
} int main()
{
while(cin>>n>>m,m)
{
getchar();
int i,j;
cnt=;
for(i=;i<=n;i++)
{
scanf("%s",Map[i]+);
}
for(i=;i<=n+;i++)
Map[i][]=Map[i][m+]='*';
for(i=;i<=m+;i++)
Map[][i]=Map[n+][i]='*';
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(Map[i][j]=='@')
{dfs(i,j); cnt++;}
}
}
cout<<cnt<<endl;
}
return ;
}

DFS or BFS --- 连通块的更多相关文章

  1. D. Lakes in Berland (DFS或者BFS +连通块

    https://blog.csdn.net/guhaiteng/article/details/52730373 参考题解 http://codeforces.com/contest/723/prob ...

  2. codeforces 590C C. Three States(bfs+连通块之间的最短距离)

    题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...

  3. 经典DFS问题 oilland 连通块

    #include "iostream" #include "cstdio" using namespace std; ][]={{,},{,-},{,},{-, ...

  4. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  5. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  6. Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  7. 有关dfs、bfs解决迷宫问题的个人见解

    可以使用BFS或者DFS方法解决的迷宫问题! 题目如下: kotori在一个n*m迷宫里,迷宫的最外层被岩浆淹没,无法涉足,迷宫内有k个出口.kotori只能上下左右四个方向移动.她想知道有多少出口是 ...

  8. UVa 1103 (利用连通块来判断字符) Ancient Messages

    本题就是灵活运用DFS来求连通块来求解的. 题意: 给出一幅黑白图像,每行相邻的四个点压缩成一个十六进制的字符.然后还有题中图示的6中古老的字符,按字母表顺序输出这些字符的标号. 分析: 首先图像是被 ...

  9. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

随机推荐

  1. vim操作:打开多个文件、同时显示多个文件、在文件之间切换

    打开多个文件: 1.vim还没有启动的时候: 在终端里输入 vim file1 file2 ... filen 便可以打开所有想要打开的文件 2.vim已经启动 输入 :open file 可以再打开 ...

  2. 学习笔记之知识图谱 (Knowledge Graph)

    Knowledge Graph - Wikipedia https://en.wikipedia.org/wiki/Knowledge_Graph The Knowledge Graph is a k ...

  3. Sharding-Jdbc概念与使用技巧

    1. Sharding-Jdbc概念与使用技巧 此讲解版本为4.0.0-RC1,目前最新的版本 2019年5月21日发布 1.1. 绑定表 指分片规则一致的主表和子表.例如:t_order表和t_or ...

  4. 存货?交期?产能不足?APS系统帮你完成计划排程

    信息化时代的今天,技术的进步.全球化的竞争与市场环境迅速变化,使得制造业企业的经营环境变得日益复杂. 集中表现在产品生命周期和交货期的缩短,与此同时顾客的需求也变得多样化和个性化.生产方式也从大批量生 ...

  5. qtp安装和使用

    QTP许可证密钥的破解步骤: 以前使用QTP9.2 使用此方法成功破解,现在本人使用的HP QuickTest Professional 11 英文版,也成功适用. 一.准备工作: 1. 由于注册码文 ...

  6. 互联网企业级监控系统 OpenFalcon

    Open-Falcon 人性化的互联网企业级监控系统,Open-Falcon 整体可以分为两部分,即绘图组件.告警组件.其中: 安装绘图组件 负责数据的采集.收集.存储.归档.采样.查询.展示(Das ...

  7. selenium中三大窗口切换

    我们在做UI自动化时,不得不会遇到一些窗口跳转与弹框,在这种的时候如果不进行切换的话,继续执行脚本必然会报错,所以我们就需要用到切换窗口的方法. selenium中主要是三种窗口 Windows窗口 ...

  8. MySQL服务启动时显示本地计算机上的MySQL服务启动后停止。某些服务在。。。

    之前一直用的好端端的,这次启动服务突然就报了这错误. 更好的阅读体验可访问 这里. 起因 为了使用 LOAD_FILE 函数,在数据库配置文件 my.ini的 [mysqld] 里添加 secure_ ...

  9. S3C2440_LCD控制器

    1.LCD控制器主要有两方面的功能: 1)从framebuffer中取出某个像素的数据: 2)配合其他信号,一起将这个数据发送给LCD 不管是2440,还是其他型号的ARM芯片.它们的LCD控制器的功 ...

  10. 禁用wordpress模板默认样式

    我们知道wordpress主题比如twentytwenty都会有样式,如果不想使用它们的默认样式怎么处理呢?其实很简单,随ytkah一起来看看吧.进入2020主题的function.php文件,里面有 ...