http://poj.org/problem?id=1562

                                                                                                   Oil Deposits
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12595   Accepted: 6868

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 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

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
题目大意:如果'@'周围的8个方向有'@'就认为它们是同一油田。求总油田的个数。
本题是dfs的入门题,我的dfs只能用很烂来形容,dfs利用了栈的思想,先进后出。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define N 1000001
using namespace std;
int n,m;
char map[][];
int v[][];
int jx[]={,,,-,-,-,,};
int jy[]={-,,,-,,,-,};
void dfs(int x,int y)
{
v[x][y]=;
int tx,ty;
for(int i=;i<;i++)
{
tx=x+jx[i];
ty=y+jy[i];
if(tx>=&&tx<n&&ty>=&&ty<m&&map[tx][ty]=='@'&&v[tx][ty]==)
{
dfs(tx,ty);
}
}
}
int main()
{
int ans=;
while(scanf("%d%d",&n,&m)!=EOF&&n!=)
{
ans=;
for(int i=;i<n;i++)
{
scanf("%*c%s",map[i]);
}
memset(v,,sizeof(v));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(map[i][j]=='@'&&v[i][j]==)
{
dfs(i,j);
ans++;
} }
}
printf("%d\n",ans); }
return ;
}

Oil Deposits(poj 1526 DFS入门题)的更多相关文章

  1. HDU 1241 连通块问题(DFS入门题)

    Input The input file contains one or more grids. Each grid begins with a line containing m and n, th ...

  2. Oil Deposits HDU - 1241 (dfs)

    Oil Deposits HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting undergr ...

  3. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

  4. POJ 3984(DFS入门题 +stack储存路径)

    POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  5. hdu 1241 Oil Deposits (一次dfs搞定有某有)

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

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

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

  7. [HDU]1016 DFS入门题

    题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...

  8. 【紫书】Oil Deposits UVA - 572 dfs求联通块

    题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...

  9. UVa 572 - Oil Deposits (简单dfs)

    Description GeoSurvComp地质调查公司负责探測地下石油储藏. GeoSurvComp如今在一块矩形区域探測石油.并把这个大区域分成了非常多小块.他们通过专业设备.来分析每一个小块中 ...

随机推荐

  1. OpenCV——轮廓特征描述

    检测出特定轮廓,可进一步对其特征进行描述,从而识别物体. 1. 如下函数,可以将轮廓以多种形式包围起来. // 轮廓表示为一个矩形 Rect r = boundingRect(Mat(contours ...

  2. openldap slapd.conf参数

    已安装系统的 /etc/openldap/slapd.conf 中包含 LDAP 服务器的完整配置文件.在此简述了其中的各个项并说明了必要的调整.以符号 (#) 为前缀的项处于非活动状态.必须取消这个 ...

  3. 【C#新特性】不用out ref同时返回多个值-元组Tuple

    元组Tuple,它是一种固定成员的泛型集合 下面先看看官方的一个使用例子: 创建一个包含7个元素的Tuple数组 // Create a 7-tuple. , , , , , );// Display ...

  4. 【抓包分析】Charles和 夜神模拟器 对安卓应用进行抓包分析

    准备工具 : 1 Charles   : https://www.charlesproxy.com  (收费) 2 夜神模拟器  : https://www.yeshen.com  (免费) 2 模拟 ...

  5. String 类实现 以及>> <<流插入/流提取运算符重载

    简单版的String类,旨在说明>> <<重载 #include <iostream> //#include <cstring>//包含char*的字符 ...

  6. OpenStack网络详解

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. Openstack需要对网络有一些了解才能进入openstack的世界,很多都是 ...

  7. vue开发 - 将方法绑定到window对象,给app端调用

    通过jsBridge方法,H5可以调用客户端(ios,android)的内部方法,同样,客户端也需要能调用H5页面里定义的js方法,但是在vue里,所有的方法都是在组件内部声明的,也只能在组件内部调用 ...

  8. [转]了解如何通过reverse_iterator的base得到iterator

    转自:http://blog.csdn.net/shuchao/article/details/3705252 调用reverse_iterator的base成员函数可以产生“对应的”iterator ...

  9. [右键]如何添加Sublime为右键菜单

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text\command] @=&q ...

  10. 【巷子】---Mock---基本使用

    一.mock解决的问题 开发时,后端还没完成数据输出,前端只好写静态模拟数据.数据太长了,将数据写在js文件里,完成后挨个改url.某些逻辑复杂的代码,加入或去除模拟数据时得小心翼翼.想要尽可能还原真 ...