DFS:POJ1562-Oil Deposits(求连通块个数)
Oil Deposits
Time Limit: 1000MS
Memory Limit: 10000K
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 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
are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
Sample Output
0
1
2
2
解题心得:
- 很简单一边找一边标记就可以了。
#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn = 110;
char maps[maxn][maxn];
bool vis[maxn][maxn];
int dir[8][2] = {1,0,-1,0,0,1,0,-1,1,-1,-1,1,-1,-1,1,1};
int n,m,ans;
void pre_maps()
{
for(int i=1;i<=n;i++)
scanf("%s",maps[i]+1);
}
bool check(int x,int y)
{
if(x<1 || y<1 || x>n || y>m)
return false;
return true;
}
void dfs(int x,int y)
{
vis[x][y] = true;
for(int i=0;i<8;i++)
{
int x1 = x+dir[i][0];
int y1 = y+dir[i][1];
if(check(x1,y1) && maps[x1][y1] == '@' && !vis[x1][y1])
dfs(x1,y1);
}
}
void DFS()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(maps[i][j] == '@' && !vis[i][j])
{
ans++;
dfs(i,j);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m) && n+m)
{
memset(vis,0,sizeof(vis));
ans = 0;
pre_maps();
DFS();
printf("%d\n",ans);
}
}
DFS:POJ1562-Oil Deposits(求连通块个数)的更多相关文章
- P1197 [JSOI2008]星球大战 [删边求连通块个数]
展开 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的 ...
- 求连通块个数 - BFS、DFS、并查集实现
本文基于leetcode的200.岛屿数量(题目
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
- HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...
- HDU1241 Oil Deposits —— DFS求连通块
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
随机推荐
- Codeforces Round #388 (Div. 2) D
There are n people taking part in auction today. The rules of auction are classical. There were n bi ...
- 微信支付——基于laravel框架的php实现
现在经手的几乎每个项目都支持微信支付,简单记录下接入的大致流程. 1.首先商户等申请各种账号,微信支付商户号,APPID,API密钥,Appsecret 2.app端上传支付需要的各个字段 3.后台收 ...
- mac下启动lnmp的方式
mac air 系列由于各种原因使得我不得不重启,重启后发现lnmp全部都关闭了, nginx: sudo nginx php-fpm: sudo php-fpm mysql: cd /usr/l ...
- Gym 101149I I - It's the Police
http://codeforces.com/gym/101149/problem/I 考虑下面这个例子 4 3 1 2 1 3 1 4 应该是选 0 0 1 1这样是最优的,我们不选1号,因为如果选1 ...
- HTML 5的革新——语义化标签(一)HTML 5的革新——语义化标签(二)
HTML 5的革新之一:语义化标签一节元素标签. 在HTML 5出来之前,我们用div来表示页面章节,但是这些div都没有实际意义.(即使我们用css样式的id和class形容这块内容的意义).这些标 ...
- Glide图片框架
//加载圆形图片Glide.with(this).load(WSCAppStatic.WEB_KEFU_PHOTO_URL+ "?usercode=8120000315") .as ...
- Windows定时任务管理以及服务管理
1.NSSM.exe https://nssm.cc/ 2.Topshelf 引用地址:https://www.cnblogs.com/guogangj/p/10093102.html#4136330
- COGS 36. 求和问题
时间限制:1.2 s 内存限制:128 MB [问题描述] 在一个长度为n的整数数列中取出连续的若干个数,并求它们的和. [输入格式] 输入由若干行组成,第一行有一个整数n ...
- grep, egrep, fgrep - 打印匹配给定模式的行
总览 SYNOPSIS grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] 描述 DESC ...
- 引入了junit为什么还是用不了@Test注解
pom文件明明引入了unit,为什么还是用不了@Test? 配置如下: <dependency> <groupId>junit</groupId> <arti ...