ACM:油田(Oil Deposits,UVa 572)
/*
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)的更多相关文章
- Oil Deposits UVA - 572
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- 油田 (Oil Deposits UVA - 572)
题目描述: 原题:https://vjudge.net/problem/UVA-572 题目思路: 1.图的DFS遍历 2.二重循环找到相邻的八个格子 AC代码: #include <iostr ...
- 【紫书】Oil Deposits UVA - 572 dfs求联通块
题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
- 油田 Oil Deposits
油田 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/L 题意: 输入一个m行n列的字符矩形,统计字符 ...
- 洛谷 题解 UVA572 【油田 Oil Deposits】
这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- uva 572 oil deposits——yhx
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...
随机推荐
- mysql8.0.11修改root密码,其他创建用户和删除用户
1.7. 查询用户密码: 查询用户密码命令:mysql> select host,user,authentication_string from mysql.user; host: 允许用户登录 ...
- CentOS 7 安装SVN并整合HTTP访问
#!/bin/bash## -------------------------------------------------## 安装svn并整合http访问## ----------------- ...
- 设置全局git忽略文件 gitconfig
cat ~/.gitconfig [user] email = yuanhuikai@liquidnetwork.com name = yuanhuikai[core] excludesfile = ...
- ODbgScript 2.01帮助文档
-------------------------------ODbgScript original pluginhttp://github.com/odbgscript--------------- ...
- 解决pymongo里操作IOSDate类型的问题
pymongo是Python对MongoDB的操作库.但是由于python没有IOSDate类型,所以对Mongo的时间类型是个很麻烦的操作.整理一个把python能识别的date类型转化为IOSDa ...
- Please install [clang](http://clang.llvm.org/) or check configuration `clang.executable`
解决方法: 1.安装clang 第一步:首先打开VScode编辑器 第二步:点击左侧“应用商店”栏 第三步:在“应用商店搜索拓展”栏输入关键字“clang” 第四步:安装提示的“C/c++ clang ...
- mysql语法 -- concat函数
mysql CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL.或许有一个或多个参 ...
- Angular4学习笔记(二)-在WebStorm中启动项目
点击配置 创建 选择命令 package.json 运行 查看运行结果
- 树莓派集群实践——nfs
1.安装 apt-get install nfs-common nfs-kernel-server 省略(sudo apt-get install portmap --->install rp ...
- 19迭代模式Iterator
一.什么是迭代模式 Iterator模式也叫迭代模式,是行为模式之 一,它把对容器中包含的内部对象的访问委让给 外部类,使用Iterator(遍历)按顺序进行遍历 访问的设计模式. 二.不使用迭代模式 ...