解题思路:第一道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. Log4Net快速配置

    1. Log4NET的概念: a) 级别:trace.debug.info.warn.error.fatal.常用debug(调试信息,程序员临时跟踪执行,在正式运行的项目中应该不显示):warn(警 ...

  2. 不得了,微软原生提供 AI 人工智能 API,而且面向网页开放

    微软原生人工智能(AI) API 不得了,微软原生提供 AI 人工智能 API,而且面向网页开放

  3. javascript动画函数封装

    function animate(obj, target) { clearInterval(obj.timer); obj.timer = setInterval(function () { var ...

  4. X Macro

    30年前我念大学时从一个朋友那里学来的一个技巧. 它是汇编语言的一个宏,但很容易转换为C语言宏. 我一直在使用它,但有意思的是我还从没在别人的代码中看到过.现在该我把这个小技巧传递下去了. 让我们举个 ...

  5. uva 11624 Fire! 【 BFS 】

    按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...

  6. Python测试(二)

    # 1.计算1-300之间所有能被3和7整除的所有数之和# num = 0# for i in range(1,300):# if i%3 ==0 and i%7 ==0:# num += i# pr ...

  7. Git的选项参数

    git的选项参数 MisSa@DESKTOP-PIQ06QO MINGW64 ~ $ git usage: git [--version] [--help] [-C <path>] [-c ...

  8. Git diff 代码比较的高级技巧

    Git diff 代码比较的高级技巧 作者:offbye 出处:http://blog.csdn.net/offbye/article/details/6592563 Git是使用branch来管理不 ...

  9. nginx1.15.8源码安装

    useradd www -s /sbin/nologin -M yum -y install gcc  pcre-devel openssl-devel cd /usr/local/src wget ...

  10. Unity图集分割

    using System.IO;using UnityEngine;using UnityEditor; public class TestSaveSprite{ [MenuItem("LL ...