您的位置:首页 > 其它

题解—detecting underground oil deposits

2018-03-20 15:40 288 查看
Problem Description
The GeoSurvComp geologicsurvey 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 equipmentto 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 containnumerous pockets. Your job is to determine how many different oil deposits are contained in a grid. <br>
geosurvcomp地质调查公司负责探测地下石油储量。GeoSurvComp的作品与一个大的矩形区域的土地在一个时间,并创建一个网格把土地分成很多广场地块。然后,它分别分析每个情节,使用感测设备,以确定是否含有石油阴谋。一块含有石油的地块叫做口袋。如果两个口袋相邻,那么它们是同一个石油矿床的一部分。石油储量可能相当大,可能包含许多口袋。你的工作是确定网格中包含多少种不同的石油储量。<br>
Input
The input file contains one ormore grids. Each grid begins with a linecontaining m and n, the number of rows and columns in the grid, separated by asingle 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 charactercorresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.<br>
// 输入文件包含一个或多个网格。每个网格以包含m和n的行开始,网格中的行数和列数由一个空格分隔。如果m=0,则表示
//输入的结束;否则为1=< = 100和1 < < = < = 100。下面是每行n个字符的m行(不包括行字符的结尾)。每一个字符对
//应一个图,并且是“*”,表示没有油,或“@”,表示一个油袋。
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 notcontain more than 100 pockets.<br>
//对于每一个网格,输出不同的油藏数量。两个不同的口袋是同一油藏的一部分,如果它们是水平的、垂直的或对角的。不超过100个口袋。                         
Sample Input1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output0
1
2
2

//原谅我的粗糙的百*翻译,,实在是太多的生词看不下去了(羞愧&&英语会努力的)
解题思路:
一开始想练一练广搜来着,结果敲着敲着又变成了深搜,,看题解时发现了一个挺好的遍历八个方向的方法,如下:for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
if(i!=0||j!=0)
{

}PS:
1-输入的格式是字符串而不是字符
AC.1#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
char map[101][101];
int m=0,n=0,ans;
int mark[101][101]={0},
dx[]={-1,-1,-1,0,0,1,1,1},
dy[]={-1,0,1,-1,1,-1,0,1};

void search(int x,int y,int id)
{
if(x<0||x>=m||y<0||y>=n)
return ;
if(mark[x][y]>0||map[x][y]!='@')
return ;
mark[x][y]=id;
for(int i=0;i<=7;i++)
search(x+dx[i],y+dy[i],id);
}

int main()
{
while(scanf("%d%d",&m,&n)&&(m+n)!=0)
{
ans=0;//清零
memset(mark,0,sizeof(mark));
for(int i=0;i<m;i++)
scanf("%s",map[i]);//注意其中没有空格,需用字符串输入

for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if((mark[i][j]==0)&&(map[i][j]=='@'))//没有被访问过
search(i,j,++ans);
}
cout<<ans<<endl;
n=0;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  深搜 查找联通块