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 ...
随机推荐
- vxlan 简单理解 vs calico 网络模型
1.vxlan(virtual Extensible LAN)虚拟可扩展局域网,是一种overlay的网络技术,使用MAC in UDP的方法进 行封装,共50字节的封装报文头. 2.VTEP为虚拟机 ...
- qmlscene: could not find a Qt installation of ''
qt └── helloqt.qml 在qt目录下运行helloqt.qml文件时出现这个提示: $ qmlscene helloqt.qml qmlscene: could not find a Q ...
- #define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)
((GPIO_TypeDef *) GPIOA_BASE)表示将GPIOA_BASE强制转换为指针类型的结构体, #define GPIOA ((GPIO_TypeDef *) GPIOA_BASE) ...
- [SDOI2011]工作安排 BZOJ2245
分析: 费用流裸题,按照题面要求建边就可以了,语文题,我读了10多分钟才知道这题干啥...特别是注意一个细节a[j+1]-a[j]... 附上代码: #include <cstdio> # ...
- hung task机制
最近在修改内核源码的时候一直出现格式化磁盘的时候,进程会出现状态D,看内核日志会看到如下信息: INFO: task filebench: blocked seconds. Oct :: localh ...
- WPF listview Test Message list
UI: <Window x:Class="WoZhuLianyuanTool.SendContentsWind" xmlns="http://schemas.mic ...
- 20155217《网络对抗》Exp03 免杀原理与实践
20155217<网络对抗>Exp03 免杀原理与实践 实践内容 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用shellcode编程 ...
- Exp1 PC平台逆向破解(5)M
Exp1 PC平台逆向破解(5)M [ 直接修改程序机器指令,改变程序执行流程] 用命令cp pwn1 20155320备份pwn1 输入objdump -d 20155320反汇编,找到call指令 ...
- 《Flask Web开发实战:入门、进阶与原理解析(李辉著 )》PDF+源代码
一句话评价: 这可能是市面上(包括国外出版的)你能找到最好的讲Flask的书了 下载:链接: https://pan.baidu.com/s/1ioEfLc7Hc15jFpC-DmEYBA 提取码: ...
- CODE[VS] 1159 最大全0子矩阵
写一道CODEVS的题目 其实我还是很喜欢CODEVS的界面的 主要是系统地学习一下悬线法这个看似十分简单,实际就是十分简单的算法 对于一些详细的东西参考dalao's blog,不喜勿喷 对于悬线法 ...