http://acm.hdu.edu.cn/showproblem.php?pid=1241
#include<bits/stdc++.h> using namespace std; const int MAX=2000+10; int n,m; char mmp[MAX][MAX]; //此处的dfs只是搜索出连通块所以需要在原图上改变; int dis[8][2]= {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}}; void dfs(int x,int y) { int tx,ty; for(int i=0; i<8; i++) { tx=x+dis[i][0]; ty=y+dis[i][1]; if(mmp[tx][ty]=='@') { mmp[tx][ty]='*'; dfs(tx,ty); } } } int main() { while(cin>>n>>m) { if(n==0) break; int ans=0; for(int i=0; i<n; i++) for(int j=0; j<m; j++) cin>>mmp[i][j]; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(mmp[i][j]=='@') ans++,mmp[i][j]='*',dfs(i,j); } cout<<ans<<endl; } return 0; }