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. Python3内建函数sorted

    排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大小是没有意义的 ...

  2. JavaWeb 发送邮件

    我们可以使用第三方的邮箱服务器来发送邮件. 常用的邮件传输协议有2种:POP3/SMTP.IMAP/SMTP. POP和IMAP的区别:在邮箱客户端的操作,比如移动邮件.标记已读,如果使用POP,是不 ...

  3. HTTP是什么,不是什么?

    HTTP是什么? 不是什么? HTTP: 超文本传输协议,也就是HyperText Transfer Protocol.这是从字面上的理解,如果深一步的理解呢? HTTP又是什么呢? HTTP是什么 ...

  4. Linux的权限管理操作-Linux从入门到精通第七天(非原创)

    文章大纲 一.权限概述二.权限设置三.属主与属组设置四.扩展五.学习资料下载六.参考文章 一.权限概述 总述:Linux系统一般将文件可存/取访问的身份分为3个类别:owner.group.other ...

  5. JS数据打印的几种方式

    /** * 在页面输出内容! */ document.write("This's Great!"); /** * 控制台打印: */ console.log("This ...

  6. node 淘宝镜像

    永久使用 打开终端执行 npm config set registry https://registry.npm.taobao.org 临时使用 npm --registry https://regi ...

  7. @TableField

    @TableField 描述:字段注解(非主键) 属性 类型 必须指定 默认值 描述 value String 否 "" 字段名 el String 否 "" ...

  8. Kibana 学习资料

    Kibana 学习资料 网址 Kibana 官方文档 https://s0www0elastic0co.icopy.site/guide/en/kibana/current/introduction. ...

  9. 12-赵志勇机器学习-Label_Propagation

    (草稿) 过程: 1. 初始化所有节点的 labels 成唯一的值: 2. 对每个节点,将 label 更新为和其相连的所有节点中,标签最多的 节点的label: 2. 初始化情况下,假如所有相连的节 ...

  10. 常用dos命令(4)

    系统管理at 安排在特定日期和时间运行命令和程序shutdown立即或定时关机或重启taskkill结束进程(WinXPHome版中无该命令)tasklist显示进程列表(Windows XP Hom ...