您的位置:首页 > 其它

Oil Deposits (dfs)深度搜索

2017-07-24 18:41 232 查看

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32295    Accepted Submission(s): 18740Problem DescriptionThe 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 eachplot 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 numerouspockets. Your job is to determine how many different oil deposits are contained in a grid.  InputThe 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. Followingthis 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. OutputFor 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 SourceMid-Central USA 1997题意:英语不好, 能懂题意就行。说@是一个有油的地方,@与@相邻就表示同一个油口袋,斜的也算;比如@**@   这两个@也是同一个油口袋;这题是一道典型的dfs的题,每次从有@的地方开始dfs向八个方向深搜,直到找不到@为止,将访问过的位置做标记,下次不再访问;看看能进行几个dfs就会有几个油口袋;
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int ivs[110][110];//访问的标志,0为未访问,1为访问过了 
char a[110][110];
//方向数组 
int b[]={1,-1,0,0,1,-1,1,-1};
int q[]={0,0,1,-1,-1,1,1,-1};
int r,c;
int dfs(int x,int y)
{
int i,j;
ivs[x][y]=1;
//访问了 
//向x,y的8个方向深度搜索 
for(i=0;i<8;i++)
{
int ax=x+b[i];
int ay=y+q[i];
//查看ax,ay是否越界 以及是否被访问了 
if(ax>=1&&ax<=r&&ay>=1&&ay<=c&&a[ax][ay]=='@'&&
4000
;!ivs[ax][ay])
{
dfs(ax,ay);
}
}
return 0;
}
int main()
{
while(cin>>r>>c,r&&c)
{
int i,j;
//初始化ivs 
memset(ivs,0,sizeof(ivs));
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
cin>>a[i][j];
//统计次数 
int sum=0;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(a[i][j]=='@'&&!ivs[i][j])
{
dfs(i,j);
ivs[i][j]=1;
sum++;
}
}
}
cout<<sum<<endl;
}
return 0;
}
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息