HDU_1241 Oil Deposits

 
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
 
题目大意:

地质urvcomp地质勘测公司负责探测地下油层。GeoSurvComp每次处理一个大的矩形区域,并创建一个网格,将土地划分为许多方形地块。然后用传感设备分别分析每个地块,以确定该地块是否含有石油。一块含有石油的土地叫做口袋。如果两个储层相邻,则它们属于同一油层的一部分。石油储量可以相当大,可能包含许多口袋。你的工作是确定有多少不同的石油蕴藏在一个网格。

输入

输入文件包含一个或多个网格。每个网格都以包含m和n的一行开始,m和n是网格中行和列的数量,由一个空格分隔。如果m = 0,则表示输入结束;否则1 <= m <= 100, 1 <= n <= 100。下面是m行,每行有n个字符(不包括行尾字符)。每个角色对应一个情节,要么是“*”,代表没有油,要么是“@”,代表一个油袋。

输出

对于每个网格,输出不同的石油沉积物的数量。如果两个不同的储层在水平、垂直或对角方向相邻,那么它们就是同一个油层的一部分。一个石油矿床不会包含超过100个口袋。

样例输入
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
 
样例输出
0 1 2 2
 #include<bits/stdc++.h>
using namespace std; char martrix[][];
int move[][]={,,-,,,,,-,,,-,-,,-,-,};//两个坐标一组 分为8组
int h,w;
void dfs(int x,int y)//定义dfs函数,主函数找到了@,dfs启动,寻找主函数找到的@八面存在的@
{
int next_x,next_y,i;
martrix[x][y]='*';//把找到的@变为*,以免重复搜索
for(i=;i<;i++)
{
next_x=x+move[i][];//[0]表示两个坐标一组的第一个 [i]表示两个坐标一组的第几组
next_y=y+move[i][];//[1]表示两个坐标一组的第二个 [i]表示两个坐标一组的第几组
if(next_x>=&&next_x<h&&next_y>=&&next_y<w)
{
if(martrix[next_x][next_y]=='@')
{
dfs(next_x,next_y);
}
}
}
} int main()//主函数开始,寻找第一个@
{
freopen("input.txt","r",stdin);
int i,j,sum;
while(scanf("%d%d",&h,&w)&&(h!=||w!=))
{
for(i=;i<h;i++)
scanf("%s",martrix[i]);
sum=;
for(i=;i<h;i++)
{
for(j=;j<w;j++)
{
if(martrix[i][j]=='@')
{
dfs(i,j);//转移到dfs函数,由dfs函数开始搜索主函数找到@的八面存在的@
sum++;
}
}
}
printf("%d\n",sum);
}
fclose(stdin);
return ;
}

(DFS)HDU_1241 Oil Deposits的更多相关文章

  1. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  2. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  3. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  4. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  5. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  7. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 搜索——深度优先搜索(DFS)

    设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...

随机推荐

  1. 在CSS中,link里 的rel="stylesheet"是什么意思?

    rel是relationship的英文缩写,它描述了当前页面与href所指定文档的关系:stylesheet就是样式表的意思:CSS是 Cascading Style Sheet(级联样式表)的缩写 ...

  2. FZU-Problem 2150 Fire Game(两点bfs)

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

  3. many connection errors,更改max_connection_errors的值

    https://www.cnblogs.com/tonyccc/p/11496101.html https://blog.csdn.net/li_li_lin/article/details/7276 ...

  4. Redis 数据总结 (2.命令实现逻辑)

    1.通过合理的Redis数据分布,实现逻辑的简化,即将部分逻辑纳入redis 连个sort表的合并,相关的资料见 http://www.redis.net.cn/order/3613.html ZIN ...

  5. AntDesign(React)学习-13 Warning XX should not be prefixed with namespace XXX

    有篇UMI入门简易教程可以看看:https://www.yuque.com/umijs/umi/hello 程序在点击操作时报了一个Warning: [sagaEffects.put] User/up ...

  6. 自定义输入函数 - C语言编程

    有返回功能的输入函数: #include <stdio.h> #include <conio.h> void input(char content[]); int main() ...

  7. 并查集路径压缩优化 UnionFind PathCompression(C++)

    /* * UnionFind.h * 有两种实现方式,QuickFind和QuickUnion * QuickFind: * 查找O(1) * 合并O(n) * QuickUnion:(建议使用) * ...

  8. 杭电oj 2072————统计单词数(java)

    problem:统计单词数 思路:利用HashMap的特性——不能反复存储同一个键得数据,所以可以保证map里边儿的元素都是不重复的,存储完毕之后直接输出size就好了 注意事项: 1.利用strin ...

  9. 移动端安卓IOS对接H5项目遇到的坑

    最近做一个H5项目 退出的时候需要调用原生的弹框退出, 之前以为很简单,直接ajax调用后端退出接口即可.后来发现数据拿不到,因为状态用的是cookie存储, 后来想到ajax 传参数请求,后来还是拿 ...

  10. normalization, standardization and regularization

    Normalization Normalization refers to rescaling real valued numeric attributes into the range 0 and ...