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
src:https://vjudge.net/contest/136571#problem/A
要注意dfs到数组边界停止
#include<bits/stdc++.h>
using namespace std;
int n,m;
char x[104][104];
int vis[104][104];
int sum=0;
bool adj(int n1,int n2,int i,int j){
if((n1==i&&abs(n2-j)==1)||(n2==j&&abs(n1-i)==1))
return 1;
if(abs(n1-i)==1&&abs(n2-j)==1)
return 1;
return 0;
}
void dfs(int n1,int n2){
if(n1==0||n2==0||n1==n+1||n2==m+1)
return;
vis[n1][n2]=1;
for(int i=n1-1;i<=n1+1;i++){
for(int j=n2-1;j<=n2+1;j++){
if(!vis[i][j])
dfs(i,j);
}
}
}
void f(int r,int c){
sum=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=r;i++){
scanf("%s",x[i]+1);//from 1!
}
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++){
if(x[i][j]=='*')
vis[i][j]=1;
}
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++){
if(!vis[i][j]){
sum++;
dfs(i,j);
}
}
cout<<sum<<endl;
}
int main(){
cin>>n>>m;
while(m!=0){
f(n,m);
cin>>n>>m;
}
return 0;
}