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
问题:找连通块...最简单DFS 和前面那道水塘找连通一样...甚至只是改了改符号...
#include<cstdio>
#define Max_N 10010
#define Max_M 10010
int N,M;
char field[Max_N][Max_M+1];
void input(){
for(int i=0;i<N;i++){
scanf("%s",&field[i]);
}
}
void dfs(int x,int y){
//把当前点从@改成*
field[x][y]='*';
for(int dx=-1;dx<=1;dx++){
for(int dy=-1;dy<=1;dy++){
//向各个方向移动
int nx=x+dx,ny=y+dy;
//判断条件,看是否在范围内
if(0<=nx&&nx<N&&0<=ny&&ny<M&&field[nx][ny]=='@') dfs(nx,ny);
}
}
}
void solve(){
int res=0;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(field[i][j]=='@'){
dfs(i,j);
res++;
}
}
}
printf("%d\n",res);
}
int main(){
while(scanf("%d %d",&N,&M)!=EOF&&N&&M){
getchar();
input();
solve();
}
return 0;
}