首页 技术 正文
技术 2022年11月21日
0 收藏 563 点赞 2,294 浏览 2450 个字

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

0122 问题分析:依旧是用的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 ;
}

   

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777