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. C - Between the Offices

    Problem description As you may know, MemSQL has American offices in both San Francisco and Seattle. ...

  2. Android常见错误整理

    1.当我new class的时候,提示以下错误: Unable to parse template "Class" Error message: This template did ...

  3. Android 在线升级

    1.获取当前安装版本 //获取当前版本 public int getVersion(Context context){ ; try { version = context.getPackageMana ...

  4. DeltaFish 校园物资共享平台 第六次小组会议

    DeltaFish 校园物资共享平台 第六次小组会议 记录人:娄雨禛 2018.6.3 任务进度(2018.5.28-2018.6.3) 前端 李鑫:商品详情界面设计.总体配色分析 刘鼎乾:卖家页面初 ...

  5. JNDI连接池连接Oracle数据库

    今天做了一个评论的小功能,要求用JNDI连接池连接Oracle数据库,以前只是测试了是否连接的上,现在没想到一个JNDI连接池连接Oracle数据库,纠结了好久,原来都是Oracle数据库的问题,这是 ...

  6. dispatch_sync

    dispatch_sync does two things: queue a block blocks the current thread until the block has finished ...

  7. git_仓库

    本地仓库 仓库(repository)可以理解成一个目录,这个目录里面的所有文件都可以被git管理起来,每个文件的修改删除git都能进行跟踪. 创建一个空目录---进入文件下---查看当前路径,当前路 ...

  8. html第八节课

    导航 1.首先在<head>里面引用一个JQUERY的文件以用来制作鼠标点击动画效果(从网站上下载即可) 1 <script language="javascript&qu ...

  9. android下xml放哪儿?

    1.用Project->Deployment,打开发布文件窗口,增加要发布的文件.然后设置文件发布的位置Remote Path,填写为assets\internal\ 2.代码 varp: st ...

  10. JDK源码阅读-Integer

    先上一版字符串转数值的几个方法的区别 parseInt(String s),解析字符串数,10进制,返回int parseInt(String s, int radix),解析字符串数,radix为指 ...