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 ...
随机推荐
- day61
Vue 八.重要指令 v-bind <!-- 值a --> <div v-bind:class='"a"'></div> <!-- 变量a ...
- STM32 & FreeRTOS & KFIFO (巧夺天工)
巧夺天工 的 KFIFO ,用STM32实现. 实现源文件如下: /********************************************************** * * 文件名 ...
- Docker一键部署Hadoop心得(一)
最近一直在折腾使用docker一键部署全分布式hadoop集群,虽然一键部署的脚本写好了并且可以成功运行出各个节点,但在运行一个wordcount实例时出现了错误,错误如下: java.io.IOEx ...
- Java面试题,Java三大特性之一——多态的理解
首先我们知道Java是一门面向对象的语言 面向对象三大特性,封装.继承.多态. 封装.继承.多态 ↓ 无论是学习路线,还是众人的口语习惯,都是按照这个这样进行排序,这是有原因的.因为封装好了才能继承, ...
- 汇编 shr 逻辑右移指令,shl 逻辑左移指令,SAL 算术左移指令,SAR 算术右移指令
知识点: shr 逻辑右移指令 shl 逻辑左移指令 一.SHL 逻辑左移指令测试 shr 逻辑右移指令 右移一位相当于整除2 shl 逻辑左移指令 左移一位相当于乘2 //很多时候会溢出 //& ...
- 汇编 for循环
知识点: for循环生成代码1 for循环生成代码2 inc指令 一.一般情况下的for循环汇编代码分析 ;i<=;i++) { printf("%d,",i); } ...
- Partition4:增加分区
在关系型 DB中,分区表经常使用DateKey(int 数据类型)作为Partition Column,每个月的数据填充到同一个Partition中,由于在Fore-End呈现的报表大多数是基于Mon ...
- flask_admin 笔记三 客户化视图
客户化视图1, model数据模型参数配置1)配置全局参数内置的ModelView类很适合快速入门. 但是,您需要配置其功能以适合您的特定型号. 这是通过设置ModelView类中提供的配置属性的值来 ...
- nginx 新增域名访问
nginx 新增域名访问 1.申请阿里云域名 2.指向阿里云主机 3.配置nginx文件 server { listen 80; server_name zlx.test.com; set $root ...
- 3Sum(or k_Sum)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...