题意:求连通块个数。

分析:dfs。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int cnt;
int m, n;
int vis[MAXN][MAXN];
char a[MAXN][MAXN];
int judge(int i, int j){
return i >= && i < m && j >= && j < n;
}
void dfs(int x, int y){
vis[x][y] = ;
for(int i = ; i < ; ++i){
int tmpx = x + dr[i];
int tmpy = y + dc[i];
if(judge(tmpx, tmpy) && !vis[tmpx][tmpy] && a[tmpx][tmpy] == '@'){
dfs(tmpx, tmpy);
}
}
}
int main(){
while(scanf("%d%d", &m, &n) == ){
if(!m && !n) return ;
memset(vis, , sizeof vis);
cnt = ;
for(int i = ; i < m; ++i)
scanf("%s", a[i]);
for(int i = ; i < m; ++i){
for(int j = ; j < n; ++j){
if(a[i][j] == '@' && !vis[i][j]){
++cnt;
dfs(i, j);
}
}
}
printf("%d\n", cnt);
}
}

UVA - 572 Oil Deposits(dfs)的更多相关文章

  1. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  2. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  3. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

  4. UVa 572 - Oil Deposits (简单dfs)

    Description GeoSurvComp地质调查公司负责探測地下石油储藏. GeoSurvComp如今在一块矩形区域探測石油.并把这个大区域分成了非常多小块.他们通过专业设备.来分析每一个小块中 ...

  5. poj1562 Oil Deposits(DFS)

    题目链接 http://poj.org/problem?id=1562 题意 输入一个m行n列的棋盘,棋盘上每个位置为'*'或者'@',求'@'的连通块有几个(连通为8连通,即上下左右,两条对角线). ...

  6. UVa 572 Oil Deposits (Floodfill && DFS)

    题意 :输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横竖以及对角方向),就是说它们属于同一个八连块. 分析 :可以考虑种子填充深搜的方法.两重for循 ...

  7. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  8. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  9. ZOJ 1709 Oil Deposits(dfs,连通块个数)

    Oil Deposits Time Limit: 2 Seconds      Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...

随机推荐

  1. sass 使用入门教程

    我们都知道,css没有变量,也没有条件语句,在开发过程中,难免有些麻烦,因此有了CSS预处理器(css preprocessor),Sass便是其中之一. 一.什么是Sass Sass (Syntac ...

  2. Ecshop图片不清晰怎么办?

    很多人说缩略图的质量不高,模糊,那是因为gd库生成缩略图时,默认生成jpg缩略图或商品图的质量是75.可以通过修改生成缩略图质量的默认值来提高缩略图的质量. 找到includes/cls_image. ...

  3. JNI-使用RegisterNatives注册本地方法

    转自: http://blog.chinaunix.net/uid-26009923-id-3410141.html 1. 以前在jni中写本地方法时,都会写成 Java_com_example_he ...

  4. jqueryui.position.js源代码分析

    近期要写前端组件了.狂砍各种组件源代码,这里分析一款jqueryui中的posistion插件,注意,它不是jqueryui widget,首先看下源代码整体结构图 1.看到$.fn.position ...

  5. git codes

    https://github.com/chibi-guts/DressUpProject https://github.com/TuttiFruttiFT/TFAndroid https://gith ...

  6. 区域医疗移动医疗影像解决方案2--基于FLEX的PACS

    基于Flex的PACS和基于HTML5的PACS,都不是基于DICOM的WADO的方式,即所有的图像操作,移动.缩放.旋转.测量.伪彩.窗宽窗位调整等都是在本地浏览器能够完成,不用和服务器进行频繁的交 ...

  7. 去掉html标签和空格等

    <?php$str = '<span style="color:#f00;">good; world</span>';echo $str.'<b ...

  8. 高级Bash脚本编程指南

    http://www.cnblogs.com/rollenholt/archive/2012/04/20/2458763.html

  9. Gradle的安装与配置

    Gradle的下载地址:http://gradle.org/gradle-download/ Tips:要先安装JDK并配置其环境变量. 下载完成后将里面的文件解压出来,如下图所示: 添加一个名称为G ...

  10. Spring – ${} is not working in @Value--转载

    原文:http://www.mkyong.com/spring/spring-is-not-working-in-value/ By mkyong | February 4, 2015 | Last ...