Oil Deposits(油田)(DFS)
题目:
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides 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 containsone 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
题意:
和迷宫的输入差不多,‘*’是墙,‘@’是油田,以一个油田为中心,如果它的东南西北一个各个角落,一共八个方向也有油田的话,这些油田就算作一个油田。
分析:
查找到‘@’的位置,找到一个,油田加1,对找到的‘@’的四周进行DFS查找,刚开始我以为和迷宫的做法差不多,我把查找过的地方用一个数组标记一下,后来测试结果不对,
才发现在某一次存在题意中的相邻油田时,在主函数中会加1,而实际上这块油田我已经计算进去了,这样就重复了。正确的做法应该是把计算进去的那块油田变为‘*’,这样
就不存在重复计算了!!!
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
char a[][];
int row,col;
int dir[][]=
{
{,},
{,},
{,-},
{,-},
{,},
{-,},
{-,},
{-,-}
};
using namespace std;
void dfs(int i,int j)
{
a[i][j]='*';
for (int k=;k<;k++)
{
int x=i+dir[k][];
int y=j+dir[k][];
if (x>=&&x<=row&&y>=&&y<=col&&a[x][y]=='@')
dfs(x,y);
}
return ;
}
int main()
{
while ((cin>>row>>col)&&(row!=||col!=))
{
int c=;
getchar();
for (int i=;i<=row;i++)
for (int j=;j<=col;j++)
cin>>a[i][j];
for (int i=;i<=row;i++)
for (int j=;j<=col;j++)
if (a[i][j]=='@')
{
dfs(i,j);
c++;
}
cout << c << endl;
}
return ;
}
Oil Deposits(油田)(DFS)的更多相关文章
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
- HDOJ/HDU 1241 Oil Deposits(经典DFS)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- ZOJ 1709 Oil Deposits(dfs,连通块个数)
Oil Deposits Time Limit: 2 Seconds Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...
- 【HDU - 1241】Oil Deposits(dfs+染色)
Oil Deposits Descriptions: The GeoSurvComp geologic survey company is responsible for detecting unde ...
- HDU 1241 Oil Deposits【DFS】
解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- Oil Deposits(DFS连通图)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
随机推荐
- Go语言集成开发环境之GoLand安装使用
下载Go语言开发包 大家可以在Go语言官网(https://golang.google.cn/dl/)下载 Windows 系统下的Go语言开发包,如下图所示. 这里我们下载的是 64 位的开发包,如 ...
- UI Automation编程辅助工具Inspect的下载和使用
UIAutomation微软提供的UI自动化库,主要用AutomationElement类来表示UI 自动化目录树中的一个UI自动化元素,.NET Windows的窗体应用程序和WPF应用程序. In ...
- 长沙中考2019数学T25讲解
好久没更Blog了... 为了应付完成寒假作业,还是更一下(再不更都庚子年了) Upd:2020.1.22 题目 第一问 还是比较水友好的 给顶点就相当于多给了对称轴-\(\frac{b}{2a}\) ...
- 【iOS入门】UITableView加载图片
学习带图片的列表 官方 LazyTableImages demo http://download.csdn.net/detail/jlyidianyuan/5726749 分析源码是学习的好方法. ...
- 14 微服务电商【黑马乐优商城】:day06-了解vue-router和webpack的使用
本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...
- 唐顿庄园S01E01观看感悟
刚刚看了唐顿庄园的第一季第一集.看第一遍的时候,主要是看剧情,看看有没有什么吸引人的.我是一带而过的.等到看第二遍的时候,仔细观察画面,欣赏画面的美感,琢磨人物的台词对话.不断的倒退回放,越看越有滋味 ...
- 蓝桥杯练习Day 2
问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...
- Error: Invalid or corrupt jarfile SpringBootTemplate.jar
当在尝试将SpringBoot打包成为Jar文件, 丢到linux服务器去运行的时候, 尝试在windows自带的CMD窗口命令行中运行jar文件的时候, 遇到了这样的问题. 错误的意思是: 无效 或 ...
- 《VSTO开发中级教程》刘永富 著 清华大学出版社 在线购买
现在可以和作者 刘永富 通过“二手书直卖”这个APP直接买书. 二手书直卖 的下载方法: 方法一:加QQ群61840693,群共享中搜索“二手书直卖”,下载后打开即可. 方法二:从本帖下载:二手书直卖 ...
- script和scriptreplay_超骚气的实时监控你的服务器
今天看到一个超级叼的linux命令,可以完整记录屏幕上的命令与输出结果. 有人问这有什么叼的,不就是保存历史操作记录吗?我看看日志也能看出来. 不不不,我要说的“完整记录”包括第几秒执行什么命令,就像 ...