上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--depth first search,话不多说,直接上题了解:

Description:
某石油勘探公司正在按计划勘探地下油田资源,工作在一片长方形的地域中。他们首先将该地域划分为许多小正方形区域,然后使用探测设备分别探测每一块小正方形区域内是否有油。若在一块小正方形区域中探测到有油,则标记为’@’,否则标记为’*’。如果两个相邻区域都为1,那么它们同属于一个石油带,一个石油带可能包含很多小正方形区域,而你的任务是要确定在一片长方形地域中有多少个石油带。 所谓相邻,是指两个小正方形区域上下、左右、左上右下或左下右上同为’@’。
Input:
输入数据将包含一些长方形地域数据,每个地域数据的第一行有两个正整数m和n,表示该地域由m*n个小正方形所组成,如果m为0,表示所有输入到此结束;否则,后面m(1≤m≤100)行数据,每行有n(1≤n≤100)个字符,每个字符为’@’或’*’,表示有油或无油。每个长方形地域中,’@’的个数不会超过100。
Output:
每个长方形地域,输出油带的个数,每个油带值占独立的一行。油带值不会超过100。
Sample Input:
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@**@
*@**@
@@@*@
@@**@
0 0
Sample Output:
0
1
2
2

思路:

搜索到一个“@”后开始搜索其周围的“@”一直到无法搜索到为止,从第一个“@”开始将其本身和其他与之连接的“@”变为成“ * ”,防止重复。

实验代码:

 #include<bits/stdc++.h>

 using namespace std;

 char maps[][];//定义油田
int m,n,cnt=;//cnt为“感染”次数(油田数量) void dfs(int x,int y)
{
if(maps[x][y]!='@'||x<||y<||x>m||y>n)//判断是否越界和是否为油田
return; else
{
maps[x][y]='*'; //将其标记
for(int i=-;i<=;i++)
for(int j=-;j<=;j++)
dfs(x+i,y+j);//将其为中心的周围八格进行搜索
} } int main (int argc,const char * argv[])
{
while(scanf("%d %d",&m,&n))
{
if(m==||n==)return ;
cnt=;
for(int i=;i<m;i++)
scanf("%s",maps[i]); for(int i=;i<m;i++)
for(int j=;j<n;j++)
if(maps[i][j]=='@')//搜索油田
{
cnt++;//每进入一次则代表一块油田
dfs(i,j);
}
cout<<cnt<<endl;
}
return ;
}

题目--oil Deposits(油田) 基础DFS(深度搜索)的更多相关文章

  1. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  2. Oil Deposits HDU - 1241 (dfs)

    Oil Deposits HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting undergr ...

  3. [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  4. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  6. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

  7. hdu 1241 Oil Deposits (一次dfs搞定有某有)

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  8. UVa 572 - Oil Deposits (简单dfs)

    Description GeoSurvComp地质调查公司负责探測地下石油储藏. GeoSurvComp如今在一块矩形区域探測石油.并把这个大区域分成了非常多小块.他们通过专业设备.来分析每一个小块中 ...

  9. HDU 1241 Oil Deposits(经典DFS)

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

随机推荐

  1. ios-密码判断

    我们经常在项目时有涉及到用户或是手机号登录,这时一般会配合密码才能登录成功. 下面发一些关于手机和密码形式的判断: - (void)registButtonClick:(id)sender { )// ...

  2. SharpDevelope 在 Windows 7 SP1 with .net framework4.0 下编译时找不到resgen.exe 解决办法

    如果在vs下编译正常,在SharpDevelope下编译报这个错误,可以更改编译时的.netframework版本和C#版本.在 Tool->Project Upgrade 进行项目转换后,一般 ...

  3. java_字段声明

    多字段继承,为避免混淆,simple name与qualified name的使用 package java20180129_1; interface Frob { float v=2.0f; } c ...

  4. WebForm AnyWay

    项目地址 :  https://github.com/kelin-xycs/WebFormAnyWay WebForm AnyWay 用 WebForm 构建 简洁高效 的 Web 应用 WebFor ...

  5. 加密与解密md5 3des

    /// <summary> /// MD5加密 /// </summary> /// <param name="s"></param> ...

  6. 深入分析JavaWeb Item2 -- Tomcat服务器学习和使用

    https://segmentfault.com/a/1190000004095363 一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件 ...

  7. 初始nginx(启动运行) 使用nginx做一个简单的静态资源服务器

    第一次接触nginx的时候,那时候公司还是用的一些不知名的小技术,后来公司发展问题,重新招了人,然后接触到nginx,公司 使用nginx用来做代理服务器,所有请求 都先经过nginx服务器,然后交由 ...

  8. 常量&字符编码

    day1 name='Nod Chen' name2=name print('My name is ',name,name2) name='Luna zhou' print(name,name2) _ ...

  9. [UE4]Border

    Border: 边界; 边; 镶边; 包边; Border也是一个容器,只能包含一个子元素. 一.添加一个名为testBorder的UserWidget,添加一个Border到默认成Canvas Pa ...

  10. 使用Mechanize实现自动化表单处理

    使用Mechanize实现自动化表单处理   mechanize是对urllib2的部分功能的替换,能够更好的模拟浏览器行为,在web访问控制方面做得更全面 mechanize的特点: 1 http, ...