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

典型的BFS问题;

AC代码为:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
int vis[][];
int m, n;
char str[][];
int bfx[] = { ,-,,,,,-,- };
int bfy[] = { ,,,-,,-,,- };
queue<int> q;
void BFS(int i, int j)
{
while (!q.empty())
q.pop();
q.push(i*n + j); while (!q.empty())
{
int u = q.front();
q.pop();
int cx = u / n;
int cy = u % n; for (int k = ; k<; k++)
{
int nx = cx + bfx[k];
int ny = cy + bfy[k]; if (nx >= && nx<m && ny >= && ny<n && !vis[nx][ny] && str[nx][ny] == '@')
{
vis[nx][ny] = ;
q.push(nx*n + ny);
}
}
}
} int main()
{ while (~scanf("%d%d", &m, &n), m || n)
{ memset(vis, , sizeof(vis));
int sum = ;
for (int i = ; i<m; i++)
{
scanf("%s", str[i]);
}
for (int i = ; i<m; i++)
{
for (int j = ; j<n; j++)
{
if (str[i][j] == '@' && !vis[i][j])
{
vis[i][j] = ;
BFS(i, j);
sum++;
}
}
} printf("%d\n", sum);
} }

ZOJ-1709的更多相关文章

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

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

  2. ZOJ 1709 Oil Deposits(dfs,连通块个数)

    Oil Deposits Time Limit: 2 Seconds      Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...

  3. CSU-ACM2018暑假集训6—BFS

    可以吃饭啦!!! A:连通块 ZOJ 1709 Oil Deposits(dfs,连通块个数) B:素数变换 打表+bfs POJ 3216 Prime Path(打表+bfs) C:水bfs HDU ...

  4. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  5. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  6. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  7. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  8. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  9. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  10. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

随机推荐

  1. Windows系统下搭建WAMP环境

    Wamp就是Windos Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件.其中php环境配置是至关重要的一部分,本文就针对php在本地的 ...

  2. [LC] 700题 Search in a Binary Search Tree (二叉搜索树中的搜索) (二叉搜索树)

    ①中文题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 在上述示例 ...

  3. nuxt.js 注册全局组件

    plugins 属性配置 src: String (文件的路径) ssr: Boolean (默认为 true) 如果值为 false,该文件只会在客户端被打包引入. 根目录找到 nuxt.confi ...

  4. Github相关知识

    github的提交流程 mkdir 目录名      :创建一个空文件夹 mkdir webs webs代表创建的新文件名称 cd 目录名   :切换到文件夹 cd webs 切换到当前新建的目录下 ...

  5. 力扣(LeetCode)验证回文字符串II 个人题解

    给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: ...

  6. 力扣(LeetCode)删除排序链表中的重复元素II 个人题解

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 思路和上一题类似(参考 力扣(LeetCode)删除排序链表中的重复元素 个人题解)) 只不过这里需要用到一个前 ...

  7. Centos7安装redis5.0.7

    1. 安装依赖包 yum install -y gcc gcc-c++ 2. 下载最新版redis安装包并解压安装 cd /usr/local/src wget http://download.red ...

  8. React动画库

    npm i react-transition --save import {CSSTransition} from 'react-transition-group'

  9. 如何在maven中下载jar包

    1.进入maven网址  https://mvnrepository.com/ 2.搜索你想要的包的名字 3.找到列表中你想要的包 4.点击一个版本 5.点击 view all 6.找到你想要的包,点 ...

  10. mac如何开启两个vmware虚拟机

    转载链接:https://blog.csdn.net/aifore/article/details/87833088