Oil Deposits

Time Limit: 1000MS

Memory Limit: 10000K

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

Sample Output

0

1

2

2


解题心得:

  1. 很简单一边找一边标记就可以了。

#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn = 110;
char maps[maxn][maxn];
bool vis[maxn][maxn];
int dir[8][2] = {1,0,-1,0,0,1,0,-1,1,-1,-1,1,-1,-1,1,1};
int n,m,ans; void pre_maps()
{
for(int i=1;i<=n;i++)
scanf("%s",maps[i]+1);
} bool check(int x,int y)
{
if(x<1 || y<1 || x>n || y>m)
return false;
return true;
} void dfs(int x,int y)
{
vis[x][y] = true;
for(int i=0;i<8;i++)
{
int x1 = x+dir[i][0];
int y1 = y+dir[i][1];
if(check(x1,y1) && maps[x1][y1] == '@' && !vis[x1][y1])
dfs(x1,y1);
}
} void DFS()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(maps[i][j] == '@' && !vis[i][j])
{
ans++;
dfs(i,j);
}
}
} int main()
{
while(scanf("%d%d",&n,&m) && n+m)
{
memset(vis,0,sizeof(vis));
ans = 0;
pre_maps();
DFS();
printf("%d\n",ans);
}
}

DFS:POJ1562-Oil Deposits(求连通块个数)的更多相关文章

  1. P1197 [JSOI2008]星球大战 [删边求连通块个数]

    展开 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的 ...

  2. 求连通块个数 - BFS、DFS、并查集实现

    本文基于leetcode的200.岛屿数量(题目

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

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

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

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

  5. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

  6. HDU1241 Oil Deposits —— DFS求连通块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

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

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

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

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

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

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

随机推荐

  1. Codeforces Round #541 (Div. 2) C.Birthday

    链接:https://codeforces.com/contest/1131/problem/C 题意: 求给的n个数,相邻差值最小的排列方式.1-n相邻. 思路: sort后隔一个取一个,取到底后再 ...

  2. 执行脚本 提示 command not found

    问题现象: 初学shell,写了个脚本, 1.从windows 写好 脚本,然后部署到 linux 上. 2.chmod +x之后执行提示command not found,系统环境redhat9,用 ...

  3. jq或zp监听input的value改变问题

    $(document).on('input propertychange','#citySelectorValue',function () { alert("s"); } 以上J ...

  4. vagrant教程

    http://blog.smdcn.net/article/1308.html http://ninghao.net/blog/1566 如何定制一个自己的 vagrant box: https:// ...

  5. jQuery的基本使用及选择器和筛选器

    回顾 事件 鼠标clickdblclickcontextmenumouseentermouseleavemousemovemousedownmouseup​键盘keydownkeyupkeypress ...

  6. vs2013编译过程中,错误 59 error C4996: 'GetVersionExW': 被声明为已否决

    好几次碰到这个错误,必须mark 一下!!!!!Project Properties > Configuration Properties > C/C++ > General > ...

  7. 【cpp】new delete

    double *M = new double[2*num]; double *T = new double[2 * num]; double *activeM = new double[2 * num ...

  8. 玄学C语言之scanf,printf

    #include <bits/stdc++.h> using namespace std; int main() { int a,c,d; ]; scanf("%d." ...

  9. valgrind测试程序内存泄漏问题

    1.用wincap将valgrind放入系统任意路径下,解压 2.  登录主机后台在需要测试程序的路径下运行此行命令: /opt/valgrind/bin/valgrind ./itb(例) 3. 跑 ...

  10. CPP-基础:wchar_t

    目 录 1简介 2例如 3将char转换成wchar_t 1.简介 wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于uni ...