Oil Deposits

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

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
 
 
 
 
刚刚学了深搜,做一下总结。
DFS
按着一条路走到,不撞南墙不回头,回的话也是回到上一次最近调用dfs()函数的地方,然后看看是否还有满足条件的,如果有则按满足条件的那个地方一直往下搜索(追根究底,一直往下,一直),直到撞南墙,把所有的地方都遍历一次。
 
小白总结,欢迎指正
 
附代码:
#include <stdio.h>
#include <string.h>
int m,n,total = ,book[][];
char c[][];
int next[][]=
{
{,},//右
{,},//右下
{,},//下
{,-},//左下
{,-},//左
{-,-},//左上
{-,},//上
{-,}//右上
}; void dfs(int x,int y)
{
int tx,ty,k;
for(k=;k<;k++)
{
tx = x+next[k][];
ty = y+next[k][]; if(tx<||ty<||tx>m||ty>n)
{
continue;
}
if(c[tx][ty]=='@'&&book[tx][ty]==)
{
//total++;//这里不能加1 ,因为是跟上一个连着的油田,都算是一块油田
book[tx][ty] = ;
dfs(tx,ty);//8个中有一个满足条件,以这一个为中心,继续往下搜,如果不满足条件,返回来,接着上一次没搜完的搜
//book[tx][ty] = 0;//每一格就走一次,搜过了不再搜
}
}
return;
}
int main()
{
int i,j;
while(scanf("%d%d",&m,&n)&&m!=)
{
getchar();
total = ;
memset(book,,sizeof(book));
for(i=;i<=m;i++){
for(j=;j<=n;j++){
scanf("%c",&c[i][j]);
}
getchar();
}
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
if(c[i][j]=='@' && book[i][j]==)
{
total++;//加它本身
dfs(i,j);//对它的八个方向进行搜索
}
}
}
printf("%d\n",total);
}
return ;
}

不撞南墙不回头———深度优先搜索(DFS)Oil Deposits的更多相关文章

  1. 深度优先搜索 DFS(Depath First Search, DFS)

    深度优先搜索是一种枚举所有完整路径以遍历所有情况的搜索方法.(不撞南墙不回头) DFS一般用递归来实现,其伪代码思路过程一般如下: void DFS(必要的参数){    if (符和遍历到一条完整路 ...

  2. 广度优先搜索(BFS)与深度优先搜索(DFS)的对比及优缺点

    深搜,顾名思义,是深入其中.直取结果的一种搜索方法. 如果深搜是一个人,那么他的性格一定倔得像头牛!他从一点出发去旅游,只朝着一个方向走,除非路断了,他绝不改变方向!除非四个方向全都不通或遇到终点,他 ...

  3. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  4. 深度优先搜索DFS和广度优先搜索BFS简单解析

    转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...

  5. 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)

    需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...

  6. 【算法入门】深度优先搜索(DFS)

    深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...

  7. 深度优先搜索 DFS 学习笔记

    深度优先搜索 学习笔记 引入 深度优先搜索 DFS 是图论中最基础,最重要的算法之一.DFS 是一种盲目搜寻法,也就是在每个点 \(u\) 上,任选一条边 DFS,直到回溯到 \(u\) 时才选择别的 ...

  8. 用深度优先搜索(DFS)解决多数图论问题

    前言 本文大概是作者对图论大部分内容的分析和总结吧,\(\text{OI}\)和语文能力有限,且部分说明和推导可能有错误和不足,希望能指出. 创作本文是为了提供彼此学习交流的机会,也算是作者在忙碌的中 ...

  9. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

随机推荐

  1. phpstudy安装好之后mysql无法启动(亲测可行)

    安装好phpstudy后,Apache可以启动,Mysql无法启动. 尝试解决办法:可能是之前已经装过Mysql,要把系统服务里面的MySQL删除,留下MySQLa服务. 在cmd命令行下输入:sc ...

  2. Dockerfile 编写

    转: https://blog.fundebug.com/2017/05/15/write-excellent-dockerfile/如何编写最佳的Dockerfile 译者按: Dockerfile ...

  3. file.length()方法返回0字节

    本地调试ok 扔到服务器上就返回0 初步估测是因为使用jnotify监控文件修改事件的时候,刚好文件被重命名了(下载完成后xxx.tmp,被改成正确的后缀名) 2019-7-20:不是上边说的那个亚子 ...

  4. 【教程】5分钟在PAI算法市场发布自定义算法

    概述 在人工智能领域存在这样的现象,很多用户有人工智能的需求,但是没有相关的技术能力.另外有一些人工智能专家空有一身武艺,但是找不到需求方.这意味着在需求和技术之间需要一种连接作为纽带. 今天PAI正 ...

  5. 八.DBN深度置信网络

    BP神经网络是1968年由Rumelhart和Mcclelland为首的科学家提出的概念,是一种按照误差反向传播算法进行训练的多层前馈神经网络,是目前应用比较广泛的一种神经网络结构.BP网络神经网络由 ...

  6. Codeforces 3D

    题目链接 D. Least Cost Bracket Sequence time limit per test 1 second memory limit per test 64 megabytes ...

  7. C# Socket流数据大小端读写封装

      网络数据是大端模式,而c#中的数据小端结构,那么在读写网络数据的时候需要进行转换.c#类库IPAddress已经封装了大小端的转换. 封装代码如下: using System.IO; using  ...

  8. Django 点滴

    1.views 中可用 render 传递参数 def home(request): info_dict = {'site': u'震撼学习', 'content': u'各种IT技术'} #Tuto ...

  9. 2019-8-31-dotnet-core-用值初始化整个数组

    title author date CreateTime categories dotnet core 用值初始化整个数组 lindexi 2019-08-31 16:55:58 +0800 2019 ...

  10. 2019-10-18-dotnet-修复找不到-System.ServiceProcess-定义

    title author date CreateTime categories dotnet 修复找不到 System.ServiceProcess 定义 lindexi 2019-10-18 21: ...