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.
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100 + 2;
char g[maxn][maxn];
int d[maxn][maxn];
int m,n;
void dfs(int a ,int b){
d[a][b] = 1;
for(int i = -1; i <= 1; i++){
for(int j = -1; j <=1; j++){
int x = a+i;
int y = b+j;
if(x < 0 || x >= m || y < 0 || y >= n) continue;
if(g[x][y] == '@' && !d[x][y]){
dfs(x,y);
}
}
}
}
int main(){
while(scanf("%d%d",&m,&n) == 2 && m != 0){
getchar();
for(int i = 0; i < m; i++){
scanf("%s",g[i]);
}
memset(d,0,sizeof(d));
int cnt = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(g[i][j] =='@' && !d[i][j]){
dfs(i,j);
cnt++;
}
}
}
printf("%d\n",cnt);
}
return 0;
}
本文介绍了一种用于探测地下油田分布的算法实现。通过将土地划分为网格,并使用递归深度优先搜索的方法来确定不同油田的数量。每个含有石油的地块被视为一个口袋,如果两个口袋相邻,则属于同一油田。
490

被折叠的 条评论
为什么被折叠?



