Oil Deposits

Descriptions:

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

题目大意:

GeoSurvComp地质调查公司负责探测地下石油储藏。 GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块。他们通过专业设备,来分析每个小块中是否蕴藏石油。如果这些蕴藏石油的小方格相邻,那么他们被认为是同一油藏的一部分。在这块矩形区域,可能有很多油藏。你的任务是确定有多少不同的油藏。

Input

输入可能有多个矩形区域(即可能有多组测试)。每个矩形区域的起始行包含m和n,表示行和列的数量,1<=n,m<=100,如果m =0表示输入的结束,接下来是n行,每行m个字符。每个字符对应一个小方格,并且要么是'*',代表没有油,要么是'@',表示有油。

Output

对于每一个矩形区域,输出油藏的数量。两个小方格是相邻的,当且仅当他们水平或者垂直或者对角线相邻(即8个方向)。

题目链接:

https://vjudge.net/problem/HDU-1241

典型的dfs深搜题,加个染色就行,即每个大油田染上一种颜色,求出多少种颜色即可

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int n,m,num;
int vis[][];//是否能染色
char mp[][];//记录地图
int dt[][]={{,-},{,},{,},{-,},{,-},{,},{-,-},{-,}};//八个方向
void dfs(int x,int y)
{
if(mp[x][y]=='#'||vis[x][y]!=||x<||y<||x>=n||y>=m)//不满足条件就返回
return;
vis[x][y]=num;//染色
for(int i=; i<; i++)//判断往哪走
{
int tx=x+dt[i][];
int ty=y+dt[i][];
if(mp[tx][ty]=='@'&&vis[tx][ty]==&&tx>=&&tx<n&&ty>=&&ty<m)
{
dfs(tx,ty);
}
}
}
int main()
{
while(cin >> n>>m,n+m)
{
num=;
memset(vis,,sizeof(vis));
for(int i=; i<n; i++)
cin >> mp[i];
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
if(mp[i][j]=='@'&&vis[i][j]==)//搜索满足条件
{
num++;//这一块染上这个数字的颜色
dfs(i,j);//开始深搜
}
}
}
cout<< num << endl;//有几种颜色
}
}

【HDU - 1241】Oil Deposits(dfs+染色)的更多相关文章

  1. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  2. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  3. HDU 1241 Oil Deposits (DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. HDU 1241 Oil Deposits (DFS or BFS)

    链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...

  5. HDU 1241 Oil Deposits DFS搜索题

    题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...

  6. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  7. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  8. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  9. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  10. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

随机推荐

  1. 洛谷——P3353 在你窗外闪耀的星星

    P3353 在你窗外闪耀的星星 题目描述 飞逝的的时光不会模糊我对你的记忆.难以相信从我第一次见到你以来已经过去了3年.我仍然还生动地记得,3年前,在美丽的集美中学,从我看到你微笑着走出教室,你将头向 ...

  2. [Bzoj4722]由乃(线段树好题)(倍增处理模数小快速幂)

    4722: 由乃 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 360  Solved: 131[Submit][Status][Discuss] D ...

  3. 【原创】PHP扩展开发入门

    PHP扩展开发入门 作者:wf (360电商技术组) 在我们编写自己的第一个php扩展之前,先了解一下php的总体架构和执行机制. php的架构如图1所看到的. 当中一个重要的就是SAPI(serve ...

  4. Deepin-安装node

    点击下载:Linux x64 文件解压: 方式1$xz -d file.tar.xz $tar -xvf file.tar 方式2 $tar xvJf file.tar.xz 解压后,把它移动到:/u ...

  5. 分享一个检测用户是否用手机(Mobile)访问网站的 PHP 类

    有一个基于MIT License协议开源的PHP程序 http://code.google.com/p/php-mobile-detect/ 程序就是一个文件,下载之后直接引用就可以. 使用方法: & ...

  6. Jenkins系列之-—02 email-ext 邮件模板

    邮件通知配置 系统管理 → 系统设置 → 邮件通知 SMTP 服务器:配置 SMTP 服务器.(不填默认本地运行服务器) 用户默认邮件后缀:注册用户邮件只需填写用户名即可,后缀会加该后缀,如果填写,则 ...

  7. leetcode_Repeated DNA Sequences

    描写叙述: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ...

  8. 谁是性能杀手?Kafka多Topic下启用SSL时延增大问题分析

    问题背景 项目中将Kafka接口进行RESTful封装,在使用RESTful接口进行性能测试时,发现Topic数增多后,开启SSL与非SSL进行测试,发现开启SSL后性能下降得厉害.例如600个Top ...

  9. css3最新版中文参考手册在线浏览

    对于CSS 3.0,它对于我们Web设计人员来说不只是新奇的技术,更重要的是这些全新概念的Web应用给我们的设计开发提高了效率以及更多的无限可能性,我们将不必再依赖图片或者 Javascript 去完 ...

  10. Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)

    题目地址:传送门 这题尽管是DIV1的C. . 可是挺简单的. .仅仅要用线段树分别维护一下横着和竖着的值就能够了,先离散化再维护. 每次查找最大的最小值<=tmp的点,能够直接在线段树里搜,也 ...