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. 10.3 c++ STL 初步

    #include<Windows.h>#include<iostream>#include<algorithm>  // sort  swap   min   ma ...

  2. Codeforces 832A. Sasha and Sticks

    It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day h ...

  3. Codeforce 141A - Amusing Joke (sort)

    So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and have guests a ...

  4. PHP函数对比 array_merge()与加号合并数组的区别

    首先准备两个数组,从数组索引类型分别讨论. 数组索引为字符串索引时: $a = array('a' => 1, 'b' => 2, 'c' => 3); $b = array('b' ...

  5. 如何架构一个 React 项目?

    编程有点像搞园艺.比起竭力去对付BUG(虫子),我们更愿意把一切弄得整洁有序,以免最后落得个身在荒野丛林中.低劣的架构会拖我们的后腿,也会使得BUG更容易钻进系统里去. 想要对你的项目进行架构,方法有 ...

  6. Excel VBA 如何在工作表上使用Option Button按钮

    应用场景 在Excel的页面上放一个“确认”按钮,再放几个Option Button按钮,编写代码,点击“确认”按钮,判断出选择了哪个Option按钮, 然后根据选择不同的Option Button去 ...

  7. unity中添加音量控制的一些步骤

    1.先确认要控制的音源(Audio Source)所使用的输出(Output),例如我这里BGM使用的是MainMixer: 2.暴露音量(Volume)参数,让脚本可以控制.这里如果不暴露出来,脚本 ...

  8. QQ第三方登录(二)

    首先我们先来看一下我的目录 Connect2.1  是我们从下载的SDK,内容包含 其他文件在配置之后全部删除了! index.html 是我们点击登陆的页面(以下为html中的代码) <cen ...

  9. jdk8-》allMatch、anyMatch、max、min函数

    allMatch函数: 检查是否匹配所有元素,只有全部符合才返回true boolean flag = list.stream().allMatch(obj->obj.length()>5 ...

  10. Vue开发重点基础知识

    1.Vuejs组件 vuejs构建组件使用 Vue.component('componentName',{ /*component*/ }): 这里注意一点,组件要先注册再使用,也就是说: Vue.c ...