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. 885-螺旋矩阵 - III

    885-螺旋矩阵 - III 在 R 行 C 列的矩阵上,我们从 (r0, c0) 面朝东面开始 这里,网格的西北角位于第一行第一列,网格的东南角位于最后一行最后一列. 现在,我们以顺时针按螺旋状行走 ...

  2. CTF中压缩文件的常见解法

    此篇记录两篇记录的比较好的关于压缩文件破解的总结 链接:https://blog.csdn.net/xuqi7/article/details/71437882 https://blog.csdn.n ...

  3. 10.3 c++ STL 初步

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

  4. vue实现网页简单计算器实例代码

    效果: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  5. OrCAD Capture CIS绘制原理图、Allegro PCB Design XL 绘制PCB

    1.OrCAD Capture CIS绘制原理图 1.1.快捷键 (1)放置连线         w (2)放置net名称      n     放下一个时再按n可以编辑名字 (3)编辑属性      ...

  6. 题解【AcWing10】有依赖的背包问题

    题面 树形 DP 的经典问题. 我们设 \(dp_{i,j}\) 表示当前节点为 \(i\),当前节点的子树(包含当前节点)最多装的体积是 \(j\) 的最大价值. 我们遍历节点的过程就相当于做了一遍 ...

  7. Linux运维--14.Kolla部署OpenStack使用external MariaDB Galera Cluster

    使用haproxy+keepalived实现Mariadb负载均衡 controller2: 10.100.2.52 haproxy+keepalived controller3: 10.100.2. ...

  8. phpstorm格式化数组

    如果我们想要的这样的数组格式的话,可以设置 <?php $arr = array["a" => 0, "b" => 1, "c&qu ...

  9. Docker镜像加速-配置阿里云镜像仓库

    Docker默认远程仓库是https://hub.docker.com/ 比如我们下载一个大点的东西,龟速 由于是国外主机,类似Maven仓库,慢得一腿,经常延迟,破损: 所以我们一般都是配置国内镜像 ...

  10. AtCoder arc078_d Mole and Abandoned Mine

    洛谷题目页面传送门 & AtCoder题目页面传送门 给定一个无向连通带权图\(G=(V,E),|V|=n,|E|=m\)(节点从\(0\)开始编号),要删掉一些边使得节点\(0\)到\(n- ...