链接:E - Oil Deposits

题目:

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

翻译:给你一块地图 ,其中' @ '代表探测到有油的地方, 而' * '则是代表没有油的地方。
现在题目给出了油田的定义,所有连在一起的' @ ' 代表一个油田, 而连在一起的定义是有公共点或公共边。
而题目的要求是输出给你一张地图上的油田的个数。

思路:常规的搜索题,不过不再是4个方向,而是8个方向

搞下代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>//能想到的头文件都写了 害怕错
int dir[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};//坐标轴上的8个方向
char mapp[105][105];
int n,m;
void dfs(int x,int y)//dfs 深度搜索 x为横 y为纵
{
if(mapp[x][y]=='@')//将搜过的口袋标记 //如果为@ 则 标记为*
mapp[x][y]='*';
for(int i=0;i<8;i++)//八个方向搜索 所以i为8
{
int a=dir[i][0]+x;//横坐标改变
int b=dir[i][1]+y;//纵坐标
if(mapp[a][b]=='@'&&a<n&&a>=0&&b<m&&b>=0)//满足条件:是口袋且横纵坐标都在范围内 则进行搜索
dfs(a,b);
}
}
int main()
{
while(scanf("%d %d",&n,&m)&&n!=0&&m!=0)
{
int sum=0;//一开始设置为0
getchar();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
scanf("%c",&mapp[i][j]);
getchar();
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(mapp[i][j]=='@')//遇到口袋进行搜索
{
dfs(i,j);
sum++;//每次搜索完后 进行+1
}
}
printf("%d\n",sum);
}
return 0;
}

这道题的话dfs与bfs都可以,但这种题我觉得dfs显得更简单些(maybe)

顺便祝大家新年快乐。



												

Oil Deposits 新年特辑篇的更多相关文章

  1. Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. Oil Deposits(dfs)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  3. 2016HUAS暑假集训训练题 G - Oil Deposits

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

  4. uva 572 oil deposits——yhx

    Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...

  5. hdu 1241:Oil Deposits(DFS)

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

  6. hdu1241 Oil Deposits

    Oil Deposits                                                 Time Limit: 2000/1000 MS (Java/Others)  ...

  7. 杭电1241 Oil Deposits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission ...

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

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

  9. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

随机推荐

  1. SpringBoot笔记(1)

    一.Spring能做什么 微服务 响应式开发 分布式云开发 web开发 无服务开发(云) 批处理业务等 二.SpringBoot作用 能快速创建出生产级别的Spring应用 SpringBoot是整合 ...

  2. Java 学习:数据类型

    前言:Java属于强类型语言 强类型语言:要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用 优势就是安全性高,但劣势速度慢 数据类型 Java的数据类型分为两大类: 基本类型(primit ...

  3. 浅谈可持久化Trie与线段树的原理以及实现(带图)

    浅谈可持久化Trie与线段树的原理以及实现 引言 当我们需要保存一个数据结构不同时间的每个版本,最朴素的方法就是每个时间都创建一个独立的数据结构,单独储存. 但是这种方法不仅每次复制新的数据结构需要时 ...

  4. 了解mysql concat()函数

    concat(arg1,arg2,....):将形参对应字段的值组合成一个字符串 假设:现在有一张学生表(test_user) 将这三个字段组合成一个字符串作为第四个字段 select test_us ...

  5. kivy之ProgressBar、ToggleButton实操学习

    之所以将kivy的ProgressBar(进度条)与ToggleButton(切换按钮)作一篇内容来记录学习,是因为这两个内容比较简单,源码内容篇幅也少. 两个功能实例源码均以main.py+prog ...

  6. mysql忘记root密码连接本地库

    http://www.cnblogs.com/zf2011/archive/2012/03/13/2393387.html 今天想做个小项目,决定用mysql数据库,但是好久没用mysql了,也忘掉了 ...

  7. java线程day-01

    综述:下面写的是我学习java线程时做的一些笔记和查阅的一些资料总结而成.大多以问答的形式出现. 一.什么是线程? 答:线程是一个轻量级的进程,现在操作系统中一个基本的调度单位,而且线程是彼此独立执行 ...

  8. leetcode 盛水最多的容器 解析

    采用双指针法: 主要思想:定义头尾两个指针,分别向中间遍历,当相遇时结束循环.存储每一次遍历容器盛水的最大容量,并不断更新. 盛水的最大容量为 左右指针高度的最小值 乘以 左右指针的距离即宽度. 则可 ...

  9. use关键字在PHP中的几种用法

    在学习了和使用了这么多年的PHP之后,您知道use这个关键字在PHP中都有哪些用法吗?今天我们就来看一下它的三种常见用法. 1. 用于命名空间的别名引用 // 命名空间 include 'namesp ...

  10. Centos8.X 搭建Prometheus+node_exporter+Grafana实时监控平台

    Prometheus Promtheus是一个时间序列数据库,其采集的数据会以文件的形式存储在本地中,因此项目目录下需要一个data目录,需要我们自己创建,下面会讲到 下载 下载好的.tar.gz包放 ...