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 分析:板子题,求连通块个数,用DFS+种子填充法即可,直接给出代码:
const int maxm = ;

int vis[maxm][maxm], n, m;
string G[maxm]; bool inside(int x, int y) {
return x >= && x < n && y >= && y < m;
} void dfs(int x,int y, int id) {
if(vis[x][y])
return;
vis[x][y] = id;
for(int i = -; i < ; ++i) {
for (int j = -; j < ; ++j) {
if(i || j) {
int nx = x + i, ny = y + j;
if(inside(nx,ny) && G[nx][ny] == '@' && !vis[nx][ny])
dfs(nx, ny, id);
}
} }
} int main() {
while(scanf("%d%d",&n,&m) && n+m) {
getchar();
memset(vis, , sizeof(vis));
for(int i = ; i < n; ++i)
cin >> G[i];
int sum = ;
for(int i = ; i < n; ++i) {
for(int j = ; j < m; ++j) {
if(G[i][j] == '@' && !vis[i][j])
dfs(i, j, ++sum);
}
}
printf("%d\n",sum);
}
}

Day2-D-Oil Deposits-POJ-1562的更多相关文章

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

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

  2. POJ 1562 Oil Deposits (并查集 OR DFS求联通块)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14628   Accepted: 7972 Des ...

  3. [POJ] 1562 Oil Deposits (DFS)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16655   Accepted: 8917 Des ...

  4. (简单) POJ 1562 Oil Deposits,BFS。

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

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

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

  6. POJ 1562:Oil Deposits

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14462   Accepted: 7875 Des ...

  7. Oil Deposits(poj 1526 DFS入门题)

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

  8. HDU 1562 Oil Deposits

    题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...

  9. Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  10. Oil Deposits(dfs)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. Spring Boot 学习方法论-如何正确的入门 Spring Boot

    想要入门 Spring Boot,那么什么样的教程是符合初学者学习的(没有太多的Java基础但有一些程序基础或者软件编程知识). 这恰好能够勾出很多问题,比如是文章图文教程适合还是视频教程适合零基础初 ...

  2. C#上传数据到HTTP,HTTPS 代码示例

    string param = string.Format("username={0}&password={1}", account, pwd); string result ...

  3. 2019年4月22日A股暴跌行情思考

    2019年4月22日A股暴跌行情思考 原因:股指期货松绑 盘面:小幅高开,单边下跌 操作: 总结: 股指期货松绑,周末媒体YY大盘暴涨,不排除空头故意借助媒体来诱多,然开盘后暴跌. 预期过于一致时,需 ...

  4. Python学习第二十七课——写一个和Django框架的自己的框架

    MyWeb框架: from wsgiref.simple_server import make_server def application(environ, start_response): pri ...

  5. JSON 解析中遇到的坑😭

    最近做加解密遇到一个很“奇葩的问题”,解析服务端加密后的字符串 序列化 时一直报错 "json解析失败:Error Domain=NSCocoaErrorDomain Code=3840 & ...

  6. 解决CentOS下boost安装后不能使用的问题

    先说一说整个经历. 因为之前没有注意到gcc4.8.5比较旧,就已经安装好boost了,当时已经可以使用了,后来发现gcc太老了,一些软件安装需要比较新的gcc支持,所以决定升级gcc,结果boost ...

  7. ES建立索引步骤, 1,index 2.mapping 3,别名

    1.建立索引PUT /index_trans_detail 2.建立mappingPOST /index_trans_detail/type_trans_detail/_mapping{ " ...

  8. python中解方程

    from sympy import * import numpy as np from numpy import linalg # 方程中的符号 x = Symbol('x') # 计算 result ...

  9. stack的使用-Hdu 1062

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  10. 2.1 MySQL基础使用

    本文是课上资料的总结非原创没有转载地址 目录 引言 为什么需要数据库? 数据库和应用程序的关系 MySQL基础使用 一.数据库简介 1.1 简介 1.2 常见数据库管理系统 1.3 MySQL卸载 1 ...