题目描述:

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. IDAPython学习(二)

    1.常用函数 ScreenEA() 获取IDA调试窗口中,光标指向代码的地址.通过这个函数,我们就能够从一个已知的点运行我们的脚本. GetInputFileMD5() 返回IDA加载的二进制文件的M ...

  2. 【D3D】Directx12运行报错&win10无法添加【图形工具】

    “我欢欣雀跃地打开<3D programming with Directx12>,准备接受D3D的洗礼,然后就卡在了 chapter 0 .”——Liez 100%纯小白的chapter ...

  3. 用python在后端将数据写入到数据库并读取

    用python在后端将数据写入到数据库: # coding:utf- import pandas as pd from sqlalchemy import create_engine # 初始化数据库 ...

  4. 【译】第三篇 SQL Server安全主体和安全对象

    本篇文章是SQL Server安全系列的第三篇,详细内容请参考原文. 一般来说,你通过给主体分配对象的权限来实现SQL Server上的用户与对象的安全.在这一系列,你会学习在SQL Server实例 ...

  5. ES6走一波 module

    ES6模块设计思想:  尽量静态化,使得编译时就能确定模块的依赖关系,输入.输出的变量.可做静态优化. ES6模块不是对象,而是通过export命令显示指定输出的代码,再通过import命令输入 ex ...

  6. Can not deserialize instance of xxx out of START_ARRAY token

    Json 反序列化异常 Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableEx ...

  7. JAVA百度过的异常(1)

    1.---无法解析类型 javax.servlet.http.HttpServletRequest.从必需的 .class 文件间接引用了它 The type javax.servlet.http.H ...

  8. (7)Java数据结构--集合map,set,list详解

    MAP,SET,LIST,等JAVA中集合解析(了解) - clam_clam的专栏 - CSDN博---有颜色, http://blog.csdn.net/clam_clam/article/det ...

  9. python第七天,dict

    在python里边创建字典的方法有如下几种: >>> dict1= dict(((),(),(),(),())) >>> print(dict1) {, , , , ...

  10. 20165221 JAVA第一周学习心得及体会

    JAVA入门的理论学习 在JAVA2使用教程的网课学中,分为以下几个模块讲解的 JAVA的地位 JAVA的特点 安装JDK(Java Develepement Kit) Java程序的开发步骤 简单的 ...