题目链接:Oil Deposits

解析:问有多少个“@”块。当中每一个块内的各个“@”至少通过八个方向之中的一个相邻。

直接从“@”的地方開始向相邻八个方向搜索,每搜到一个格子。就将它替换成“.”,一次搜索就会搜索完一个块,记录搜索的次数为答案。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; char mz[110][110];
const int dir[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, 1, -1, -1, 1, -1, -1, 1}; //八个方向
int n, m; void dfs(int sx, int sy){
mz[sx][sy] = '.'; //搜到的位置替换成‘.’
for(int i=0; i<8; i++){
int x = sx + dir[i][0];
int y = sy + dir[i][1];
if(x >= 0 && x < n && y >= 0 && y < m && mz[x][y] == '@')
dfs(x, y);
}
return ;
} int main(){
// freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &m) == 2 && n){
for(int i=0; i<n; i++){
scanf("%s", mz[i]);
} int ans = 0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++){
if(mz[i][j] == '@'){
dfs(i, j);
ans ++; //记录搜索次数
}
}
printf("%d\n", ans);
}
return 0;
}

HDU 1241 Oil Deposits (DFS)的更多相关文章

  1. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

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

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

  3. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

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

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

  5. hdu 1241 Oil Deposits (简单搜索)

    题目:   The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...

  6. HDOJ/HDU 1241 Oil Deposits(经典DFS)

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

  7. HDU 1241 Oil Deposits【DFS】

    解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...

  8. HDU 1241 Oil Deposits(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 很经典的一道dfs,但是注意每次查到一个@之后,都要把它变成“ * ”,然后继续dfs ...

  9. poj1562 Oil Deposits(DFS)

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

随机推荐

  1. 【转】jvm类加载

    类加载机制 JVM把class文件加载的内存,并对数据进行校验.转换解析和初始化,最终形成JVM可以直接使用的Java类型的过程就是加载机制. 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的生命 ...

  2. jdbc 实现分页

    jdbc 实现分页,的实现 原理这个就不介绍了.. 总之是用jdbc 的游标移动 package com.sp.person.sql.util; import java.sql.Connection; ...

  3. HTML——meta

    http://www.cnblogs.com/jr1993/p/4542862.html

  4. spring加载classpath与classpath*的区别别

    1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件. 2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文 ...

  5. UI开发模式-容器模式

    UI开发模式-容器模式 通用容器: 配置容器.

  6. R语言学习 - 热图美化

    实际应用中,异常值的出现会毁掉一张热图.这通常不是我们想要的.为了更好的可视化效果,需要对数据做些预处理,主要有对数转换,Z-score转换,抹去异常值,非线性颜色等方式. 对数转换 为了方便描述,假 ...

  7. Spring框架系列(一)--Spring MVC基础知识

    Web项目开发过程中一般都是使用MVC(Model-View-Controller)模式,早先的Struts2到Spring MVC,再到现在Spring Boot,都是相似的思 路.Spring B ...

  8. ZOJ - 3987 - Numbers (大数 + 贪心)

    参考自:https://blog.csdn.net/u013534123/article/details/78484494 题意: 给出两个数字n,m,把n分成m份,使得以下最小 思路: 或运算只有0 ...

  9. 安迪的第一个字典(Andy's First Dictionary,Uva 10815)

    输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出.单 词不区分大小写. 样例输入: Adventures in Disneyland Two blondes were goin ...

  10. [USACO] 打井 Watering Hole

    题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conven ...