AC代码:
#include<iostream>
using namespace std;
char a[105][105];
int n,m,cnt;
int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1};
void dfs(int x,int y){
a[x][y]='.';
for(int i=0;i<8;i++){
int tx=x+dx[i],ty=y+dy[i];
if(tx<1||ty<1||tx>n||ty>m) continue;
if(a[tx][ty]=='.') continue;
dfs(tx,ty);
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]!='.'){
a[i][j]='.';
dfs(i,j);
cnt++;
}
}
}
cout<<cnt;
return 0;
}
标签:tx,ty,int,Lake,c++,continue,dfs,Counting,105
From: https://blog.csdn.net/2301_79075699/article/details/145248636