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 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,虽然说dfs更好。。。。。,但是用bfs的话要注意让它搜完再进行下一次搜索,直到把所有油田走完,。
 
注意:此题描述的退出条件有误,应该是吗n>0才会退出,而且“An oil deposit will not contain more than 100 pockets.”这句描述无意义。
 #include "iostream"
#include "queue"
using namespace std;
struct person
{
int i;
int j;
};
char o[][];
void obegin(int n,int m)
{
int i,j;
for (i=;i<=n+;i++)
for (j=;j<=m+;j++)
{
if (i*j == || i == n+ || j == m+)
o[i][j] = '*';
else
cin>>o[i][j];
}
}
int dfs(int n,int m)
{
queue <person> p;
person fir,sec;
int c=;
int f;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
if (o[i][j] == '@')
{
f=;
fir.i = i;
fir.j = j;
c++;
p.push(fir);
while (!p.empty())
{
sec = p.front();
p.pop();
for (int k=;k<=;k++)
{
switch(k)
{
case : if (o[sec.i+][sec.j] == '@')
{
fir.i=sec.i+;
fir.j=sec.j;
}break;
case :if (o[sec.i-][sec.j] == '@')
{
fir.i=sec.i-;
fir.j=sec.j;
}break;
case :if (o[sec.i][sec.j+] == '@')
{
fir.i=sec.i;
fir.j=sec.j+;
}break;
case :if (o[sec.i][sec.j-] == '@')
{
fir.i=sec.i;
fir.j=sec.j-;
}break;
case :if (o[sec.i+][sec.j+] == '@')
{
fir.i=sec.i+;
fir.j=sec.j+;
}break;
case :if (o[sec.i+][sec.j-] == '@')
{
fir.i=sec.i+;
fir.j=sec.j-;
}break;
case :if (o[sec.i-][sec.j-] == '@')
{
fir.i=sec.i-;
fir.j=sec.j-;
}break;
case :if (o[sec.i-][sec.j+] == '@')
{
fir.i=sec.i-;
fir.j=sec.j+; }break;
}
if (o[fir.i][fir.j] == '@')
{
o[fir.i][fir.j]='#';
p.push(fir);
f++;
}
if (f/ == )
{
f=;
c++;
}
}
}
}
return c;
}
int main()
{
int n,m;
while (cin>>m>>n && n)
{
obegin(m,n);
cout<<dfs(m,n)<<endl;
}
return ;
}
 
 
 

暑假集训(1)第七弹 -----Oil Deposits(Poj1562)的更多相关文章

  1. 暑假集训(2)第七弹 -----今年暑假不AC(hdu2037)

    J - 今年暑假不AC Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64 ...

  2. 暑假集训(4)第七弹——— 组合(hdu1850)

    题意概括:你赢得了第一局.魔鬼给出的第二局是,如果有N堆牌,先手的人有几种可能胜利. 问题分析:尼姆游戏,先得到n堆牌的数量异或和,再将异或和与每一个牌组的数量异或,如果结果小于原牌组数量 则可能++ ...

  3. CSU-ACM2016暑期集训训练4-BFS(F - Oil Deposits)

    F - Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  4. 暑假集训(4)第五弹——— 数论(hdu1222)

    题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A ...

  5. 暑假集训(3)第四弹 -----Frogger(Poj2253)

    题意梗概:青蛙王子最近喜欢上了另一只经常坐在荷叶上的青蛙公主.不过这件事不小心走漏了风声,被某fff团团员知 道了,在青蛙王子准备倾述心意的那一天,fff团团员向湖泊中注入大量的充满诅咒力量的溶液.这 ...

  6. 暑假集训(4)第八弹——— 组合(hdu1524)

    题意概括:你已经赢得两局,最后一局是N个棋子往后移动,最后一个无法移动的玩家失败. 题目分析:有向无环图sg值游戏,尼姆游戏的抽象表达.得到每个棋子的sg值之后,把他们异或起来,考察异或值是否为0. ...

  7. 暑假集训(4)第六弹——— 组合(poj1067)

    题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走 ...

  8. 暑假集训(4)第四弹 -----排列,计数(hdu1465)

    题意概括:嗯,纵使你数次帮助小A脱离困境,但上一次,小A终于还是失败了.那数年的奔波与心血,抵不过轻轻一指,便彻底 湮灭,多年的友谊终归走向末路.这一切重击把小A彻底击溃! 不为什么,你到底还是要继续 ...

  9. 暑假集训(4)第三弹 -----递推(Hdu1799)

    问题描述:还记得正在努力脱团的小A吗? 他曾经最亲密的战友,趁他绘制贤者法阵期间,暗中设下鬼打墙将小A 围困,并准备破坏小A正在绘制的法阵.小A非常着急.想阻止他的行动.而要阻止他,必须先破解鬼打墙. ...

随机推荐

  1. 微软2016校园招聘4月在线笔试 ABC

    题目链接:http://hihocoder.com/contest/mstest2016april1/problems 第一题:输入N,P,W,H,代表有N段文字,每段有ai个字,每行有⌊W/S⌋个字 ...

  2. Java 线程池架构原理和源码解析(ThreadPoolExecutor)

    在前面介绍JUC的文章中,提到了关于线程池Execotors的创建介绍,在文章:<java之JUC系列-外部Tools>中第一部分有详细的说明,请参阅: 文章中其实说明了外部的使用方式,但 ...

  3. 在Windows8工作站上安装可靠多播协议

    为什么要安装可靠多播协议?   答:随着因特网的发展,出现了视频点播.电视会议.远程学习.计算机协同工作等新业务.传统的点到点通信方式,不仅浪费大量的网络带宽,而且效率很低.一种有效利用现有带宽的技术 ...

  4. JS继承的几种方式

    JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一. 既然要实现继承,那么我们先定义一个父类: // 定义一个动物类 function Animal (name) { // 属性 this.n ...

  5. Ollivanders: Makers of Fine Wands since 382 BC.

    Ollivanders: Makers of Fine Wands since 382 BC.                                               Time L ...

  6. GoogleProgressBar

    https://github.com/jpardogo/GoogleProgressBar

  7. [译]信仰是怎样毁掉程序猿的How religion destroys programmers

    作者原文地址 作者John Sonmez 英文水平不够高,翻译不太准确. 翻译地址:译文 尽管文章是13年的,可是这段时间恰好看到.net开源核心之后,各种java和.net掐架. 语言之争有些牵涉到 ...

  8. C# #define

    https://msdn.microsoft.com/library/yt3yck0x.aspx 使用 #define 定义符号.当您将符号用作传递给 #if 指令的表达式时,此表达式的计算结果为 t ...

  9. gdb调试程序

    一.准备好内容vim test3.c  输入如下即可 #include <stdio.h> int func(int n) {         int sum=0,i;         f ...

  10. Cisco交换机设置管理IP

    需要准备一根CONSOLE线和带串行接口的电脑. (图1) 用CONSOLE线连接好电脑与交换机(交换机的CONSOLE口一般都有表示). 然后按照图1点“开始→程序→超级终端”会弹出来一个窗口(图2 ...