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求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
随机推荐
- HDU6440(费马小定理)
其实我读题都懵逼--他给出一个素数p,让你设计一种加和乘的运算使得\[(m+n)^p = m^p+n^p\] 答案是设计成%p意义下的加法和乘法,这样:\[(m+n)^p\ \%\ p = m+n\] ...
- Jasper_crosstab_measure_display a value of field in crosstab total row
1.create a measure <measure name="myField" class="java.lang.String"> <m ...
- centos7版本对比之前版本的部分命令差异
centos7版本下的命令和之前的centos版本的命令有些许不同,最近在电脑上用VBox安装了一个centos7版本.在做一些网卡配置和安装mysql的时候遇到了一些问题.在这里总结跟大家分享下. ...
- 【aspnetcore】在过滤器(Filter)中使用注入服务(ServiceFilter|TypeFilter)
在MVC中,AOP是很常用的功能,我们经常会使用如 ActionFilter,IAuthorizeFilter 等描述对Controller和Action进行约束和扩展,一般做法如下: public ...
- 1047 - Best couple 好题~
http://www.ifrog.cc/acm/problem/1047 思路很简单,跑一发floyd,然后再用km. 但是问题来了,这个有可能n != m.那怎么办? 其实可以补上一些不存在的点.来 ...
- spring boot & mybatis集合的坑
因为是使用的mybatis逆向工程自动生成的实体类和dao层,然后在读取某一个表的content字段时出现问题. 问题描述:在mysql数据库里可以直接查询到这个字段的内容,但是使用java相关的方法 ...
- IOSButton自定义
+ (APCCustomBackButton *)customBackButtonWithTarget:(id)aTarget action:(SEL)anAction tintColor:(UICo ...
- 微软爆料新型系统,Windows7,Windows10强势来袭
本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...
- jquery最常用的几个方法。——可删除
jquery使用手册:http://www.eduyo.com/doc/jquery/cheatsheet.html $(this).hasClass("input_money") ...
- Linux系统里让vim支持markdown格式的语法高亮
Markdown是深受程序员喜爱的一个文件格式. 然而Linux里默认的vim设置,并不支持markdown格式的语法高亮显示. 下面就来介绍如何设置使得markdown格式的文件在vim里也能享有语 ...