题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33688    Accepted Submission(s): 19585

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
 
Source

题解:

典型的dfs求连通块问题。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; char M[MAXN][MAXN];
int vis[MAXN][MAXN];
int n, m, dir[][] = {,,,,,-,-,, ,,-,,,-,-,-}; void dfs(int x, int y)
{
vis[x][y] = ;
for(int i = ; i<; i++)
{
int xx = x + dir[i][];
int yy = y + dir[i][];
if( xx>= && xx<=n && yy>= && yy<=m && M[xx][yy]=='@' && !vis[xx][yy])
dfs(xx,yy);
}
} int main()
{
while(scanf("%d%d",&n,&m) && m )
{
for(int i = ; i<=n; i++)
scanf("%s", M[i]+); int ans = ;
ms(vis, );
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(M[i][j]=='@' && !vis[i][j])
{
ans++;
dfs(i,j);
}
}
printf("%d\n", ans);
} }

HDU1241 Oil Deposits —— DFS求连通块的更多相关文章

  1. UVa572 Oil Deposits DFS求连通块

      技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...

  2. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  3. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  4. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  5. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

  6. UVA 572 dfs求连通块

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  7. 用DFS求连通块(种子填充)

    [问题] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分 ...

  8. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  9. DFS:POJ1562-Oil Deposits(求连通块个数)

    Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Description The GeoSurvComp geologic survey com ...

随机推荐

  1. [MFC] TabControl选项卡的使用

    MFC中,因项目需要使用TabControl ,使用过程中发现,MFC中的TabControl与C#的TabControl不同,不能通过属性来创建选项页,只能代码生成绑定. 以下为具体的实现方法步骤: ...

  2. LeetCode OJ--Valid Parentheses

    http://oj.leetcode.com/problems/valid-parentheses/ 对栈的考察,看括号的使用方式是否合法. class Solution { public: bool ...

  3. Java普通员工管理系统

    login GUI界面(登录) package 普通员工管理系统; import java.awt.event.ActionEvent; import java.awt.event.ActionLis ...

  4. Maven实现多个项目关联自动化构建(maven-invoker-plugin插件的使用)

    以下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_automation.html: 注意: ...

  5. Oracle RAC环境下怎样更新patch(Rolling Patch)

        Oracle RAC数据库环境与单实例数据库环境有非常多共性,也有非常多异性.对于数据库补丁的更新相同如此.都能够通过opatch来完毕.但RAC环境的补丁更新有几种不同的更新方式,甚至于能够 ...

  6. C标准提前定义宏,调试时加打印非常实用

    #include<stdio.h> int main(int argc, char *argv[]) { printf("File:[%s]\r\n", __FILE_ ...

  7. [转]JAVA集合

    1.线程安全 线程安全就是说多线程访问同一代码,不会产生不确定的结果. 2.List类和Set类List类和Set类是Collection集合接口的子接口.Set子接口:无序,不允许重复.List子接 ...

  8. SolidEdge如何打开或关闭自动标注尺寸

    工具-聪慧-自动标注尺寸                    

  9. 浅析怎样学好C语言

    今天,我能够自称是一个混IT的人,并能以此谋生,将来大家能一次谋生.都要感谢两个人:克劳德.香农和约翰.冯.诺依曼,是他们发现了全部的数字化信息,不论是一段程序,一封email.一部电影都是用一连串的 ...

  10. Android开发的环境搭建及HelloWorld的实现

    安装JDK和配置Java开发环境 http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-4321 ...