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

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

Sample Output

0
1
2
2 解题思路:这是一个简单的DFS问题,就是一个m*n的矩形里面有多少块封闭的@区间,用深搜的方法即可
程序代码:
#include <cstdio>
using namespace std;
int c[][]={{,},{,},{,},{-,},{-,},{-,-},{,-},{,-}};
char a[][];
int m,n,d;
void dfs(int i,int j)
{ a[i][j]='*';
for(int k=;k<;k++)
{
int x=i+c[k][];
int y=j+c[k][];
if(a[x][y]=='@'&&x>=&&x<=m&&y>=&&y<=n)
dfs(x,y);
}
return ;
} int main()
{
while(scanf("%d%d",&m,&n)==&&m&&n)
{
d=;
int i,j;
for( i=;i<m;i++)
scanf("%s",a[i]);
for( i=;i<m;i++)
for( j=;j<n;j++)
if(a[i][j]=='@')
{ d++;
dfs(i,j);
}
printf("%d\n",d);
}
return ;
}

暴力求解——UVA 572(简单的dfs)的更多相关文章

  1. UVa 572 油田(DFS求连通块)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. UVa 572 Oil Deposits(DFS)

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

  3. POJ 1562(L - 暴力求解、DFS)

    油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...

  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求连通块+种子填充算法)

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

  6. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  7. 2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

    这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小 ...

  8. BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~

    jrMz and angle       Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Other ...

  9. 逆向暴力求解 538.D Weird Chess

    11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...

随机推荐

  1. 第二天——hibernate讲完了

    hibernate 逐步优化 第一步 只按照步骤来提取的 jre包导入错误 第二步 继续封装,把增删改查提取出来,同时进行代码的封装 HQL语句 be stranger in the code .be ...

  2. 设置ORACLE环境变量

    sqlplus 执行不了可能原因是未设置环境变量,设置方法:  export ORACLE_HOME=/usr/local/instantclient_11_2

  3. android 如何解决模块之间的通讯的耦合问题

    使用EventBus http://wuyexiong.github.io/blog/2013/04/30/android-fragment/ http://yunfeng.sinaapp.com/? ...

  4. poj1828

    poj1828 [问题的描述]是这样的:程序猿的近亲 猴子(......)最近在进行王位争夺站. 题中使用二维坐标轴上的点(x,y)来代表猴子所占有的位置, 每只猴子占有一个坐标点.并且一个坐标点上面 ...

  5. maven项目下tomcat直接启动不了(LifecycleException)。报错如下截图

    经查,tomcat项目下的lib中没有jar包,发布的时候没有将jar包发布上去.这个问题在我的博客中以前遇到过.如何将maven的jar发布到项目中,我的博客里面有记载

  6. java基础学习总结01

    在过去的一周我们先学习了JAVA的一些基础知识和基础语法,基础知识包括:指令.程序.计算机语言.机器语言.高级语言的概念和特点,java的概念.平台.组建等,以及代码的编译过程,代码的开发流程.基础语 ...

  7. Unity 5.x---00使用重力

    Unity 5.x---00使用重力 步骤一: 打开一个工程(导入Unity自带的资源),并创建并配置好必要的GameObject ,如下图: 步骤二: 1.创建一个Cube,使其位于平面上方.    ...

  8. Codeforces 474E - Pillars

    一眼看上去非常像最长不下降子序列. 然后比赛的时候对每个答案长度为k的序列,维护最后一个数的最大值和最小值. 当时不知道为什么认为从长度最长倒推至前面不会太长,于是心满意足地敲了个O(n^2).结果T ...

  9. xml程序 个人练习1

    package cn.gdpe.xml2; import java.io.File;import java.io.FileOutputStream;import java.util.List; imp ...

  10. mysql创建存储过程中的问题

    1.在创建存储过程成功后,使用call 存储过程名执行时报错: Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_gener ...