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. Docker系列(二)组件介绍

    镜像 镜像是一个只读的模版,可以用来创建Docker容器. 容器 Docker利用容器来运行应用,容器是从镜像创建的运行实例.它可以被启动.开始.停止.删除.每个容器都是互相隔离的,保证安全的平台.可 ...

  2. 终端ls显示的配色方案

    打开~/.profile或者mac上的~/.bash_profile,加入: export CLICOLOR=1 export LSCOLORS=cxdxfxexbxegedabagacad 这是我的 ...

  3. A Tour of Go Range

    The range form of the for loop iterates over a slice or map. package main import "fmt" , , ...

  4. 关于.net的一些基础知识(一)

    一.GC工作原理: GC如其名,就是垃圾收集,当然这里仅就内存而言.Garbage Collector(垃圾收集器,在不至于混淆的情况下也成为GC)以应用程序的root为基础,遍历应用程序在Heap上 ...

  5. ONIX 实例

    <?xml version="1.0"?><!DOCTYPE ONIXMessage SYSTEM"http://www.editeur.org/oni ...

  6. J2EE 全面简介

    原文地址:http://www.ibm.com/developerworks/cn/java/j2ee/ J2EE的概念 目前,Java 2平台有3个版本,它们是适用于小型设备和智能卡的Java 2平 ...

  7. IOS程序内发短信 MFMessageComposeViewController

    文章转载地址:http://www.headsky.org/?p=63 iOS4.0新加入了MFMessageComposeViewController和MFMessageComposeViewCon ...

  8. 在IT在系统中使用多租户技术的跨部门和虚拟团队的解决方案为员工提供(草案)

    1 前言 经过多年的企业信息化建设,Office系统逐步形成有9营业场所的分部门.9专业应用子系统.20独立的信息模块.330一种方法.这些系统或模块内置于Microsoft IIS.Apache T ...

  9. android datepicker源码

    /* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  10. mysql-gdb--oracle

    https://blogs.oracle.com/ksplice/entry/8_gdb_tricks_you_should