/*
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. 关于uframe源码的一些解读

    游戏管理. GameManager单例:绑定在不同的gameobject上,还是会每次都实例化一个GameManager但是可以为每一个GameManager赋值一个已经存在的单例---------- ...

  2. Oracle---常用SQL语法和数据对象

    1.INSERT  (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字 ...

  3. WARNING: Can not get binary dependencies for file...

    环境: window7 64bit python 3.5 pyinstaller 3.2 用pyinstaller 将python文件打包成exe文件的过程中,出现了如下的错误 C:\Users\ca ...

  4. pip离线安装软件包

    1. 首先一台主机上安装所有python包,然后运行如下命令下载依赖包: pip freeze > requirements pip download -r requirements 当然可以在 ...

  5. vue用组件构建应用

    组件系统是 Vue.js 另一个重要概念,因为它提供了一种抽象,让我们可以用独立可复用的小组件来构建大型应用.如果我们考虑到这点,几乎任意类型的应用的界面都可以抽象为一个组件树: 在 Vue 里,一个 ...

  6. BigDecimal提供了8种舍入方式

    BigDecimal提供了8种舍入方式 1.ROUND_UP:舍入远离零的舍入模式.在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1).注意,此舍入模式始终不会减少计算值的大小. 2. ...

  7. Nginx 1.9+PHP5.6 环境搭建

    PHP5. 下载安装包 #wget http://mirrors.sohu.com/php/php-5.6.2.tar.gz #tar -zxf php-​ 安装php依赖的包​​ #yum inst ...

  8. jQuery的ID选择器失效问题

    jQuery的ID选择器,在同一项目别的文件中一切正常: 在当前页面,jQuery的其它功能(如:$(document).ready(function(){  alert("ok" ...

  9. MSP MCU I2C入门指南

    这是一份介绍性指南,指导你如何用超低功耗MSP微控制器 (MCU) 开始一个与I2C通信有关的项目: 简介 I2C(或称为I2C,集成电路总线)是一种两线制通信形式,主要用来在短距离.电路板间的应用中 ...

  10. golang 爬虫

    go语言,goquery,colly,chromedp,webloop等 https://www.cnblogs.com/majianguo/p/8186429.html