UVa 572 油田 (dfs)
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 mand 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 and
. 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
- 思路:
直接用dfs爆搜
实现代码:
- #include<iostream>
- #include<cstring>
- using namespace std;
- const int M = 1e2+;
- int vis[M][M];
- char mp[M][M];
- int n,m;
- void dfs(int u,int v,int d){
- if(u < ||u >= m||v < ||v >= n) return;
- if(vis[u][v]||mp[u][v]!='@') return;
- vis[u][v] = ;
- for(int i = -;i <= ;i ++){
- for(int j = -;j <= ;j ++){
- if(i==&&j==) continue;
- dfs(u+i,v+j,d);
- }
- }
- return;
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie();
- cout.tie();
- while(cin>>m>>n){
- if(n==||m==) break;
- for(int i = ;i < m;i ++)
- for(int j = ;j < n;j ++)
- cin>>mp[i][j];
- memset(vis,,sizeof(vis));
- int ans = ;
- for(int i = ;i < m;i ++){
- for(int j = ;j < n;j ++){
- if(!vis[i][j]&&mp[i][j]=='@'){
- dfs(i,j,++ans);
- }
- }
- }
- cout<<ans<<endl;
- }
- return ;
- }
UVa 572 油田 (dfs)的更多相关文章
- UVa 572 油田(DFS求连通块)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- 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个格子,也可用常 ...
- 2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)
这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小 ...
- UVA 572 (dfs)
题意:找出一块地有多少油田.'@'表示油田.找到一块就全部标记. #include<cstdio> #define maxn 110 char s[maxn][maxn]; int n,m ...
- UVA 572 dfs求连通块
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- UVA - 572 Oil Deposits(dfs)
题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...
- 暴力求解——UVA 572(简单的dfs)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
随机推荐
- jQuery的extend和fn.extend理解
参考网址:http://www.cnblogs.com/yuanyuan/archive/2011/02/23/1963287.html http://www.cnblogs.com/xuxiuyu/ ...
- 微信小程序开发 [03] 事件、数据绑定和传递
1.事件绑定 在微信小程序中,事件的绑定依附于组件,当事件触发时,就会执行事件对应的处理函数. 我们回到前几章中的例子,将index页面调整为首页(app.json中调整pages数组元素的顺序),此 ...
- Windows安装mapnik
windows安装mapnik,首先去官网下载windows压缩包:http://mapnik.org/,解压位置C:\mapnik-v2.2.0 然后下载安装python2.7,安装位置 C:\P ...
- 20155229《网络对抗技术》Exp9:Web安全基础
实验内容 Webgoat实践下相关实验. 实验步骤 WebGoat: Webgoat是OWASP组织研究出的一个专门进行web漏洞实验的应用品台,这个平台里包含了web中常见的各种漏洞,例如:跨站脚本 ...
- Luogu P1273 有线电视网
最近写DP写得比较多了 但是POJ上的题目太傻比了而且不想看英文的题面,然后就在Luogu的试炼场里找了一个DP EX专题写了一下(大概3days吧,一天一题差不多) 这是一道比较简单的DP 话说树形 ...
- POJ 2299
上课讲了下数据结构,因为暂时没找到分块的板子题,所以做一下这道题加深一下对树状数组的理解. 题意就是求逆序对,从逆序对的定义就可以看出,方法有两种:归并 or 树状数组. 感觉树状数组更高级一点,写起 ...
- 用Spring.Services整合 thrift0.9.2生成的wcf中间代码-复杂的架构带来简单的代码和高可维护性
最近一直在看关于thrift的相关文章,涉及到的内容的基本都是表层的.一旦具体要用到实际的项目中的时候就会遇到各种问题了! 比如说:thrift 的服务器端载体的选择.中间代码的生成options(a ...
- flask_admin 笔记三 客户化视图
客户化视图1, model数据模型参数配置1)配置全局参数内置的ModelView类很适合快速入门. 但是,您需要配置其功能以适合您的特定型号. 这是通过设置ModelView类中提供的配置属性的值来 ...
- tomcat设置开机自启动和后台运行
前言:当浏览器页面显示不出来的时候,重启装在服务器上的tomcat可以正常使用,是通过进入tomcat的bin目录,双击startup.bat运行启动的程序,这时会弹出启动窗口(tomcat的运行日志 ...
- 机器学习1—简介及Python机器学习环境搭建
简介 前置声明:本专栏的所有文章皆为本人学习时所做笔记而整理成篇,转载需授权且需注明文章来源,禁止商业用途,仅供学习交流.(欢迎大家提供宝贵的意见,共同进步) 正文: 机器学习,顾名思义,就是研究计算 ...