题目描述:

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模板题,套用模板即可。并记得将搜索过的’@‘转换成’*‘ 代码:
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
char a[][];
int n,m,i,j; void dfs(int x,int y)
{
a[x][y]='*';
for(int dx=-;dx<=;dx++) //以一个点为中心,判断他周围有无‘@’,一直搜索下去,直到没有相连通的‘@’
for(int dy=-;dy<=;dy++){
int nx=dx+x;
int ny=dy+y;
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='@') dfs(nx,ny);
}
return ;
} void solve()
{
int ans=;
for(i=;i<n;i++)
for(j=;j<m;j++){
if(a[i][j]=='@')
{
dfs(i,j);
ans++; //每遇到一个‘@’,就将连通快总数加1,并通过dfs把相连通的‘@’都转化成‘*’
}
}
printf("%d\n",ans);
} int main()
{
while(){
scanf("%d%d",&n,&m);
if(n==&&m==) break;
memset(a,,sizeof(a));
for(i=;i<n;i++){
scanf("%s",a[i]); //输入的时候要注意,如果输入%c,记得加getchar();
}
solve();
}
return ;
}

poj1562 Oil Deposits 深搜模板题的更多相关文章

  1. poj1562 Oil Deposits(DFS)

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

  2. nyoj20——有向无环图深搜模板

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...

  3. uva12558 Egyptian Fractions (HARD version)(迭代深搜)

    Egyptian Fractions (HARD version) 题解:迭代深搜模板题,因为最小个数,以此为乐观估价函数来迭代深搜,就可以了. #include<cstdio> #inc ...

  4. POJ 1562:Oil Deposits

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14462   Accepted: 7875 Des ...

  5. [vijos1159&洛谷1494]岳麓山上打水<迭代深搜>

    题目链接:https://vijos.org/p/1159 https://www.luogu.org/problem/show?pid=1494 这是今天的第三道迭代深搜的题,虽然都是迭代深搜的模板 ...

  6. NYoj 素数环(深搜入门)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=488 深搜模板: void dfs(int 当前状态) { if(当前状态为边界状 ...

  7. (深搜)Oil Deposits -- hdu -- 1241

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  8. HDU_1241 Oil Deposits(DFS深搜)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

  9. 暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

随机推荐

  1. Java同步注解:@ThreadSafe、@Immutable、@NotThreadSafe、@GuardedBy

    Java并发编程中,用到了一些专门为并发编程准备的 Annotation.主要包括三类: <dependency> <groupId>net.jcip</groupId& ...

  2. 按科室统计 2.181222版本 关联查询join 不对

    SQL: select t0.deptName, t0.deptId, t0.startTime, t0.endTime, IFNULL(t0.num,) as num0, IFNULL(t1.num ...

  3. Java EE之Struts2异常[No mapping found for dependency [type=java.lang.String, name='actionPackages'#java.lang.RuntimeException]【摘抄】

    本博文摘自:http://www.blogjava.net/nkjava/archive/2009/03/29/262705.html 出现这个问题,可能是添加了struts2-codebehind包 ...

  4. Hadoop之HDFS思维导图

  5. webpack配置模块的查找范围

    一般我们的入口文件会引入一下公共的样式文件,比如这样 import './style' 但是这个样式文件并不会生效呀,因为你的写法不对鸭,你要把文件的后缀名也要写 import './style.cs ...

  6. AbstractQueuedSynchronizer源码解析

    1.简介 AbstractQueuedSynchronizer队列同步器,用来实现锁或者其他同步组件的基础框架 AbstractQueuedSynchronizer使用int类型的volatile变量 ...

  7. 329 experience

    截止到现在,给我最大的冲击就是HTML没有像JAVA那样严格 可以随意搭配,换句话说 HTML要的就是效果 没有一个固定的方法 即便是代码有错误 也可以实现效果 今天的东西挺好吃 能吃的消 哈哈 开森 ...

  8. 对象转换为json格式,类似中间层API

    <一头扎进SpringMvc视频教程\<一头扎进SpringMvc>第四讲 源码\> 对象自动转换为json格式要在 spring-mvc.xml添加一个东西 ,和对应的命名空 ...

  9. RPO攻击 & share your mind

    参考文章: https://xz.aliyun.com/t/2220 http://www.thespanner.co.uk/2014/03/21/rpo/ https://www.lorexxar. ...

  10. python,获取用户输入,并且将输入的内容写到.txt

    该功能缺点是必须保证该文件不存在的情况才会成功 f=open('E:/mywork/保存文件.txt','x') def userwrite(code): if code=='w': f.close( ...