解题思路:第一道DFS的题目---

参看了紫书和网上的题解--

在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时,发现第一块油田,对它DFS,这样经过一次DFS,所有的油田都被找出来了,被记0

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14384    Accepted Submission(s): 8287

Problem 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 file 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
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they 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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char map[1000][1000];
int n,m;
void dfs(int i,int j)
{
if(map[i][j]!='@'||i<0||i>=m||j<0||j>=n)
return;
else //往八个方向搜索
{
map[i][j]='0';
dfs(i-1,j-1);
dfs(i-1,j);
dfs(i-1,j+1);
dfs(i,j-1);
dfs(i,j+1);
dfs(i+1,j-1);
dfs(i+1,j);
dfs(i+1,j+1);
}
} int main()
{
int i,j;
while(scanf("%d %d",&m,&n)!=EOF&&n&&m)
{
memset(map,0,sizeof(map));
int ans=0;
for(i=0;i<m;i++)
scanf("%s",map[i]); for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
if(map[i][j]=='@')
{
dfs(i,j);
ans++;
}
}
printf("%d\n",ans);
}
}

  

HDU 1241 Oil Deposits【DFS】的更多相关文章

  1. HDU - 1241 Oil Deposits 【DFS】

    题目链接 https://cn.vjudge.net/contest/65959#problem/L 题意 @表示油田 如果 @@是连在一起的 可以八个方向相连 那么它们就是 一块油田 要找出 一共有 ...

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

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

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

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

  4. hdu 1241:Oil Deposits(DFS)

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

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

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

  6. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

  7. HDU - 1241 Oil Deposits 经典dfs 格子

    最水的一道石油竟然改了一个小时,好菜好菜. x<=r  y<=c  x<=r  y<=c  x<=r  y<=c  x<=r y<=c #include ...

  8. HDU1241 - Oil Deposits【DFS】

    GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石 ...

  9. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

随机推荐

  1. 《图解HTTP》摘要

    网络基础TCP/IP 使用Cookie进行状态管理 HTTP首部 确保Web安全的HTTPS 1.网络基础TCP/IP 2.使用Cookie进行状态管理:HTTP是无状态协议. 3.HTTP首部 HT ...

  2. css——应用多个样式

    应用多个样式 在class中使用多个样式 在这是会有优先级关系问题 在上面的代码中,aa,bb,中的颜色会有冲突,到底显示的结果会是黄色还是绿色呢? 结果是绿色的.它是以程序执行的先后为优先级,后执行 ...

  3. LD_DEBUG

    LD_DEBUG 是 glibc 中的 loader 为了方便自身调试而设置的一个环境变量.通过设置这个环境变量,可以方便的看到 loader 的加载过程. LD_DEBUG=help ./main ...

  4. NOI 2015 寿司晚宴 (状压DP+分组背包)

    题目大意:两个人从2~n中随意取几个数(不取也算作一种方案),被一个人取过的数不能被另一个人再取.两个人合法的取法是,其中一个人取的任何数必须与另一个人取的每一个数都互质,求所有合法的方案数 (数据范 ...

  5. [读书笔记] Python 数据分析 (八)画图和数据可视化

    ipython3 --pyplot pyplot: matplotlib 画图的交互使用环境

  6. markdown图片设置

    工具:typora 1. 设置图片大小(本节引用自 https://support.typora.io/Resize-Image/) Typora允许使用<img>标签显示图像,也可用于调 ...

  7. poj 3254 Corn Fields (状压dp)(棋盘dp)

    状压dp入门题 因为当前行的状态只和上一行有关 所以可以一行一行来做 因为m <= 12所以可以用二进制来表示放了或者没有放 0表示没放,1表示放 f[i][state]表示第i行状态为stat ...

  8. Java并发和多线程1:并发框架基本示例

    Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括ThreadPool,Executor,Executors,ExecutorService,Com ...

  9. n&(n-1)位运算的妙用

    一.n-1发生了什么 ①.二进制数n,n-1后,如果最后一位是0,将向前一位借2,2-1=1.最后一位为1.如果前一位为0,将继续向前一位借2,加上本身少掉的1.则变为1.一直遇到1.减为0. 所以 ...

  10. ZOJ 3288 Domination

    D - Domination Time Limit:8000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Descr ...