Submit
Status
Practice
HDU 1241
Description
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.
http://acm.hdu.edu.cn/showproblem.php?pid=1241
题意:
上下左右斜相连是联通块
数联通块个数。。。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 110;
char map[maxn][maxn];
bool visit[maxn][maxn];
int sum;
int n,m;
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
void dfs(int i,int j){
visit[i][j]=false;
for(int k=0;k<8;k++){
int ii = i+dir[k][0],jj = j+dir[k][1];
if(map[ii][jj]=='@'&&ii>=0&&ii<n&&jj>=0&&jj<m&&visit[ii][jj])
dfs(ii,jj);
}
}
int main(){
int i,j;
while(scanf("%d%d",&n,&m)!=EOF&&m){
for(i=0;i<n;i++)
scanf("%s",map[i]);
memset(visit,true,sizeof(visit));
// cout << visit[0][0]<<endl;
sum=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
if(map[i][j]=='@'&&visit[i][j]){
sum++;
dfs(i,j);
}
}
cout<<sum<<endl;
}
return 0;
}