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. Web Storage API:localStorage 和 SessionStorage

    Web Storage API 提供了存储机制,通过该机制,浏览器可以安全地存储键值对,比使用 cookie 更加直观. 参考:https://developer.mozilla.org/zh-CN/ ...

  2. CentOS7安装jenkis

    注意:终止运行Ctrl+c , 退回到shell命令Ctrl+d 一.先检查是否有java [root@huangyh huangyh]#  rpm -qa |grep java 或 java 因为C ...

  3. Http 状态码总结

    HTTP 状态码列表 一. 1 开头 (继续执行) 服务器收到请求,需要请求者继续执行操作 100:(continue) 客户端继续请求 101:(Switching Protocols) 切换协议, ...

  4. 洛谷 CF804B Minimum number of steps

    嗯... 题目链接:https://www.luogu.org/problemnew/show/CF804B 这道题没有什么技巧,只是一道找规律的题. 首先看到“ab”可以换成“bba”,所以首先要确 ...

  5. Qt实现通用模板单例

    #include <QAtomicPointer> #include <QMutex> #include <memory> using namespace std; ...

  6. Python 爬取 热词并进行分类数据分析-[热词关系图+报告生成]

    日期:2020.02.05 博客期:144 星期三 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...

  7. hbase单机版安装

    hbase单机版安装 1.      hbase单机版安装 HBase的安装也有三种模式:单机模式.伪分布模式和完全分布式模式. hbase依赖于Hadoop和Zookeeper. 这里安装的是单机版 ...

  8. 循环读取寄存器(QSFP-DD)并且分别保存log

    #!/bin/bash ####################################################################### #Created by: Bin ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:内联子标题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. spring boot 中容器 Jetty、Tomcat、Undertow

    spring boot 中依赖tomcat <dependency> <groupId>org.springframework.boot</groupId> < ...