Oil Deposits
Time Limit : 2000/1000ms(Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 60 AcceptedSubmission(s) : 24
ProblemDescription
The GeoSurvComp geologicsurvey company is responsible for detecting underground oil deposits.GeoSurvComp works with one large rectangular region of land at a time, andcreates a grid that divides the land into numerous square plots. It thenanalyzes each plot separately, using sensing equipment to determine whether ornot the plot contains oil. A plot containing oil is called a pocket. If twopockets are adjacent, then they are part of the same oil deposit. Oil depositscan be quite large and may contain numerous pockets. Your job is to determinehow many different oil deposits are contained in a grid. <br>
Input
The input file contains one ormore grids. Each grid begins with a line containing m and n, the number of rowsand columns in the grid, separated by a single space. If m = 0 it signals theend 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-linecharacters). Each character corresponds to one plot, and is either `*',representing the absence of oil, or `@', representing an oil pocket.<br>
Output
For each grid, output thenumber of distinct oil deposits. Two different pockets are part of the same oildeposit if they are adjacent horizontally, vertically, or diagonally. An oildeposit will not contain more than 100 pockets.<br>
Sample Input
1 1<br>*<br>35<br>*@*@*<br>**@**<br>*@*@*<br>18<br>@@****@*<br>5 5<br>****@<br>*@@*@<br>*@**@<br>@@@*@<br>@@**@<br>00 <br>
Sample Output
0<br>1<br>2<br>2<br>
Source
Mid-Central USA 1997
题目大意:给定一个图,上边有*和@两种标记,其中@表示石油,好多@连在一起可以看成一个大的石油区,问在这个区域中有多少个石油区。
解题思路:广搜的入门题,一开始想的时候,就感觉广搜很靠谱,因为搜相连的,用按层来历遍是比较好的,直接从地图上的每个点开始枚举,如果有一个点为@,那么就以这个点为起始点去开始广搜,然后广搜到它相连的点都标志掉,接着再重复一开始的操作。
#include<stdio.h>
#define N 105
char map[N][N];
void dfs(int a,int b)
{
if(map[a][b]=='@')
{
map[a][b]='*';
dfs(a,b+1);
dfs(a+1,b+1);
dfs(a+1,b);
dfs(a+1,b-1);
dfs(a,b-1);
dfs(a-1,b-1);
dfs(a-1,b);
dfs(a-1,b+1);
}
else
return ;
}
int main()
{
int n,m,count;
while(scanf("%d%d",&m,&n)!=EOF)
{
count=0;
if(m==0)
break;
for(int i=0; i<=N; i++)
{
for(int j=0; j<=N; j++)
map[i][j]='*';
}
for(int i=1; i<=m; i++)
{
for(int j=0; j<=n; j++)
scanf("%c",&map[i][j]);
}
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
if(map[i][j]=='@')
{
dfs(i,j);
count++;
}
}
}
printf("%d\n",count);
}
return 0;
}