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. 周黑鸭借力MES,推进智能生产

    1.项目背景介绍 中国食品企业在经过了一些年的行业整体快速增长后,国家对食品行业的管理提出了更加严格的要求,控制更严,准入门槛提高,企业数量成下降趋势. 大中型食品企业已经需要走出国门,走向国际市场, ...

  2. Linux自有服务(2)-Linux从入门到精通第六天(非原创)

    文章大纲 一.设置主机名二.chkconfig三.ntp服务四.防火墙服务五.rpm管理(重点)六.cron/crontab计划任务(重点)七.学习资料下载八.参考文章   自有服务,即不需要用户独立 ...

  3. elasticsearch 安装 可视化工具

    一.windows下安装Elasticsearch首先计算机需要JAVA环境(已有次此环境跳过)1.java环境安装网址:http://www.oracle.com/technetwork/java/ ...

  4. 红黑树实现(c/c++)

    红黑树 简介 一直想写的一种数据结构,非常厉害的思想,插入,删除,查找,修改,都是\(log_2 n\)的时间复杂度. 比AVL更强大的是,插入删除综合效率比AVL要优秀一点. 性质 一颗红黑树是满足 ...

  5. Java JMS——消息服务

    转载请注明原文地址: https://www.cnblogs.com/ygj0930/p/10921569.html 一:什么是Java消息服务—— 消息通信接口规范 Java消息服务指的:两个应用程 ...

  6. CentOS 7 安装配置分布式文件系统 FastDFS 5.0.5

    前言 项目中用到文件服务器,有朋友推荐用FastDFS,所以就了解学习了一番,感觉确实颇为强大,在此再次感谢淘宝资深架构师余庆大神开源了如此优秀的轻量级分布式文件系统,本篇文章就记录一下FastDFS ...

  7. Matplotlib 绘制误差条图

    1.自变量的误差条 代码 import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = 'S ...

  8. 一探istio-proxy(envoy)容器里的秘密

    今天,在测试环境跑通了istio. 惭愧,是用MicroK8s跑的,其它环境,不敢呀. 基本功能都OK了. 在运行了istio-injection=enabled之后,每个pod运行时,会多一个ist ...

  9. Iconfont技术

    什么是 IconFont 顾名思义,IconFont 就是字体图标.严格地说,就是一种字体,但是,它们不包含字母或数字,而是包含符号和字形.您可以使用 CSS 设置样式,就像设置常规文本一样,这使得 ...

  10. robotframework连接mysql

    1.安装mysql数据库,并启动,创建数据库test及user表(可以自定义) 2.配置robotframework环境加载DatabaseLibrary 3.安装pymysql,下载地址:https ...