Description

GeoSurvComp地质调查公司负责探測地下石油储藏。

GeoSurvComp如今在一块矩形区域探測石油。并把这个大区域分成了非常多小块。他们通过专业设备。来分析每一个小块中是否蕴藏石油。

假设这些蕴藏石油的小方格相邻。那么他们被觉得是同一油藏的一部分。

在这块矩形区域,可能有非常多油藏。

你的任务是确定有多少不同的油藏。

Input

输入可能有多个矩形区域(就可以能有多组測试)。每一个矩形区域的起始行包括m和n。表示行和列的数量,1<=n,m<=100,假设m =0表示输入的结束。接下来是n行,每行m个字符。每一个字符相应一个小方格,而且要么是'*',代表没有油,要么是'@'。表示有油。

Output

对于每个矩形区域,输出油藏的数量。两个小方格是相邻的,当且仅当他们水平或者垂直或者对角线相邻(即8个方向)。

Sample Input

 
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5 
****@
*@@*@
*@**@
@@@*@
@@**@
0 0 

虽热非常easy ,可是也感觉陌生没做过搜索了…… 这两天训练赛也非常不在状态。

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF=110;
char cnt[INF][INF];
int vis[INF][INF];
int dir[][2]= {{1,0},{0,1},{-1,0},{0,-1},{-1,-1},{1,1},{-1,1},{1,-1}};
int n,m;
void dfs(int x,int y)
{
for(int i=0; i<8; i++)
{
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx >= 1 && tx <= n && ty>=1 && ty<=m && cnt[tx][ty]=='@' && !vis[tx][ty])
{
vis[tx][ty]=1;
dfs(tx,ty);
}
}
} int main()
{ while(cin>>n>>m,n+m)
{
for(int i=1; i<=n; i++) scanf("%s",cnt[i]+1);
int count=0;
memset(vis,0,sizeof(vis));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(cnt[i][j]=='@'&&!vis[i][j])
{
count++;
dfs(i,j);
}
} }
cout<<count<<endl;
}
return 0;
}

UVa 572 - Oil Deposits (简单dfs)的更多相关文章

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

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

  2. UVA - 572 Oil Deposits(dfs)

    题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...

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

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

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

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

  5. UVa 572 Oil Deposits(DFS)

     Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil ...

  6. uva 572 oil deposits——yhx

    Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...

  7. Uva 572 Oil Deposits

    思路:可以用DFS求解.遍历这个二维数组,没发现一次未被发现的‘@’,便将其作为起点进行搜索.最后的答案,是这个遍历过程中发现了几次为被发现的‘@’ import java.util.*; publi ...

  8. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

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

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

随机推荐

  1. xhtml1-frameset.dtd

    <!-- Extensible HTML version 1.0 Frameset DTD This is the same as HTML 4 Frameset except for chan ...

  2. 用CSS伪类制作一个不断旋转的八卦图?

    前言 介绍一下如何制作一个不断旋转的八卦图.快速预览代码及效果,点击:八卦图 代码如下: HTML部分 <!DOCTYPE html> <html> <head> ...

  3. react中withRouter解决props返回为空

    利用 react + antd 框架书写导航栏时,遇到了几个坑,分别是一级菜单和二级菜单在点击的情况下,高亮没有任何问题,但是再点击浏览器返回按钮时,却就乱套了. 1. 二级菜单中,我们可以通过 pr ...

  4. 元信息标记---meta

    位于<head></head>之间 1.设置页面关键字: <meta name="keywords" content="输入具体关键字&qu ...

  5. Hbase 简单记录

    进入hbase 客户端命令行: hbase shell 根据rowkey获取单条数据: get 'HXXT_ns:app_test_hbase', 'rowkey值'   范围查询,并指定数据量: s ...

  6. SSDB 一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis.

    SSDB 一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis. 特性 替代 Redis 数据库, Redis 的 100 倍容量 LevelDB 网络支持, 使用 C/C++ ...

  7. promise原理及使用方法

    Promise 的含义 所谓Promise ,简单说就是一个容器,里面保存着某个未来才回结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消息. re ...

  8. ApplicationLoader登录失败

    报错:Please sign in with an app-specific password. You can create one at appleid.apple.com 是因为帐号开启了双重认 ...

  9. vue.js的ajax和jsonp请求

    首先要声明使用ajax 在 router下边的 Index.js中 import VueResource from 'vue-resource'; Vue.use(VueResource); ajax ...

  10. struts与spring整合

    Spring与Struts框架整合 Spring,负责对象对象创建 Struts, 用Action处理请求 Spring与Struts框架整合, 关键点:让struts框架action对象的创建,交给 ...