/*
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 37990 Accepted Submission(s): 22039 Problem 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
****@
*@@*@
*@**@
@@@*@
@@**@ Sample Output:
0
1
2
2
*/ #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std; const int M=;
const int N=; char maps[M][N];
int visited[M][N]; void dfs(int i,int j)
{
++visited[i][j];
for(int x=-;x<=;x++)
for(int y=-;y<=;y++)
{
if(maps[i+x][j+y]=='@'&&visited[i+x][j+y]==)
{
dfs(i+x,j+y);
}
}
} int main()
{
int m,n;
while(scanf("%d %d",&m,&n)==&&m&&n)
{
for(int i=;i<m;i++)
scanf("%s",maps[i]);
memset(visited,,sizeof(visited));
int answer=;
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(maps[i][j]=='@'&&visited[i][j]==)
{
answer++;
dfs(i,j);
}
}
cout<<answer<<endl;
}
return ;
}

  比较水的一道题,难度主要在于英语阅读。这道题的思路很简单,对于首先发现是@的格子,我们有理由认为在该格子的八连通区域内很可能还有另外的@(这有点像我玩MineCraft时的挖矿过程,在发现矿产的方块周围的相邻方块很大概率还能发现同类矿产)。所以很容易想到要使用深度优先遍历检索八连通区域内的格子。涉及到dfs则必然使用递归来简化代码结构(不考虑内存限制)。所以主要代码结构就是大循环遍历找@,找到后就dfs,并且用一个标志数组来记录下访问过的格子就行了。因为这道题我第一次写完代码就AC了,所以代码中就不加注释了。

tz@COI HZAU

2018/3/21

ACM:油田(Oil Deposits,UVa 572)的更多相关文章

  1. Oil Deposits UVA - 572

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  2. 油田 (Oil Deposits UVA - 572)

    题目描述: 原题:https://vjudge.net/problem/UVA-572 题目思路: 1.图的DFS遍历 2.二重循环找到相邻的八个格子 AC代码: #include <iostr ...

  3. 【紫书】Oil Deposits UVA - 572 dfs求联通块

    题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...

  4. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  5. 油田 Oil Deposits

    油田 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/L 题意: 输入一个m行n列的字符矩形,统计字符 ...

  6. 洛谷 题解 UVA572 【油田 Oil Deposits】

    这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...

  7. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  8. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  9. uva 572 oil deposits——yhx

    Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...

随机推荐

  1. [Android实例] Activity实例StartActivity出现NullPointer异常

    [Android实例] Activity实例StartActivity出现NullPointer异常 [android实例教程] 在Android低版本(如2.3.3)中出现如下“界面跳转”的错误: ...

  2. 第三部分:Android 应用程序接口指南---第三节:应用程序资源---第一章 资源提供

    第1章 资源提供 你应该经常外部化你应用程序代码中的资源,比如图片.字符串等,这样有利于你独立处理这些资源.你也应该根据特定的设备配置提供一些可替代的资源,并且把他们分组保存在指定的路径名下.运行时, ...

  3. 【经验分享】我经历的几门MOOC

    这半年来,从1月初到6月底,在coursera上注册了4们有关数据分析/挖掘的课程.这些课程都是利用业余时间学习,每周基本上花5个小时左右.其中通过了3门,注销了一门.感觉还是学到了一些东西. 第一门 ...

  4. [k8s]k8s pod的4种网络模式最佳实战(externalIPs )

    hostPort相当于docker run -p 8081:8080,不用创建svc,因此端口只在容器运行的vm上监听 缺点: 没法多pod负载 $ cat pod-hostport.yaml api ...

  5. 基于vue-cli项目添加服务端渲染

    两个示例的git地址: 1. 我的环境 2. 方式一:使用prerender-spa-plugin插件获得SSR的效果. 2.1 说明 2.2 初始化 1 vue init webpack vue-p ...

  6. python 图片上添加文字

    import PIL from PIL import ImageFont from PIL import Image from PIL import ImageDraw #设置字体,如果没有,也可以不 ...

  7. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-6底层驱动之多路开关选择器

    视频简介:该视频介绍iCore3应用开发平台中多路开关选择器的应用,多路开关选择器的硬件电路连接以及软件实现的方法. 源视频包下载地址:链接:http://pan.baidu.com/s/1geQ4W ...

  8. GPT(保护分区)解决办法

    教你在硬盘被GPT保护分区后怎么格式化  GUID 分区表 (GPT) 作为可扩展固件接口 (EFI) 计划的一部分而引入.与 PC 以前通用的旧的主引导记录 (MBR) 分区方案相比,GPT 为磁盘 ...

  9. (诊断)No module named MySQLdb

    启动Keystone同步数据库时提示: -- :: TRACE keystone File , in dbapi -- :: TRACE keystone return __import__('MyS ...

  10. 织梦中在线显示pdf文件的方法

    如何在织梦中添加pdf文件并显示呢?下面这个教程将带领大家来操作.(注:手机版无法查看) 第一步:在系统-系统基本参数-附件设置中添加pdf格式 并且将大小调大 第二步:在核心-内容模型-普通文章中添 ...