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. kuangbin带我飞QAQ 最短路

    1. poj 1502 Mathches Game 裸最短路 #include <iostream> #include <string.h> #include <cstd ...

  2. windows搭建rabbitmq ha

    1.安装erlang22.0 rabbitmq 3.7.15 2.bin下执行命令:rabbitmq-plugins enable rabbitmq_management3.替换.erlang.coo ...

  3. H5C3--线性渐变 linear-gradient,径向渐变radial-gradient,重复渐变radial-gradient

    一.线性渐变 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  4. react前端自动化webpack配置

    1. npm init2. package.json install dependence webpack webpack-dev-server react react-dom react-hot-l ...

  5. jmeter的组件介绍--框架

    测试计划(test plan):用于存放测试脚本的容器. 线程(threads):通过java多线程来实现模拟多用户操作,只有在线程组下才能添加sample(各种协议的请求),因此线程是必须的. 取样 ...

  6. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  7. Nginx1.10.2安装配置

    以下操作均在当前用户目录操作(root的目录,截止2016.11.2最新版本) 1.安装常用工具及基础包:yum -y install wget git vim make gcc gcc-c++ op ...

  8. java swing调试时线程显示名字

    一般有一个默认名字 但是具体运行到哪一个线程,需要猜 为了节约时间,提高效率 可以给线程写个中文名(因为默认就是英文,写中文,一眼就能挑出来) 以RTC定时器为例子 final TimerRtc ti ...

  9. 关于python的元组操作

    关于元组: 元组和列表是类似的,但是元组中的数据是不可以修改的. 元组是一对 () 元组操作: 元组是不可以修改的所以对元组的操作极少 定义空元组(因为元组一旦创建,数据不可被修改,所以极少创建空元组 ...

  10. ubuntu设置终端命令历史记录

    ----------------------------------------------- HISTTIMEFORMAT='%F %T ' # 使用HISTTIMEFORMAT在历史中显示TIME ...