1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <bits/stdc++.h> using namespace std;
int n,m; int c[110][110]; int vis[110][110]; int des[9][2] = {{0,0},{1,0},{1,1},{1,-1},{-1,0},{-1,1},{-1,-1},{0,1},{0,-1}};
struct p { int x; int y; };
bool check(int x,int y) { if(x >= 1 && x <= n && y >= 1 && y <= m && vis[x][y] == 1) return 1; return 0; }
queue<p>q; int bfs(int i,int j) { p pd; pd.x = i; pd.y = j; vis[pd.x][pd.y] = 0; q.push(pd); while(!q.empty()) { p pp; pp = q.front(); q.pop(); p pz; for(int i = 1; i <= 8 ; i++) { pz.x = pp.x + des[i][0]; pz.y = pp.y + des[i][1]; if(check(pz.x,pz.y)) { vis[pz.x][pz.y] = 0; q.push(pz); } } } }
int main() { while(scanf("%d %d",&n,&m) && (n&&m)) { getchar(); for(int i = 1; i <= n ; i++) { for(int j = 1; j <= m ; j++) { scanf("%c",&c[i][j]); c[i][j] == '@'?vis[i][j] = 1:vis[i][j] = 0; } getchar(); }
int cnt = 0; for(int i = 1; i <= n ; i ++) { for(int j = 1; j <= m ; j++) { if(vis[i][j] == 1) { cnt++; bfs(i,j); } } } printf("%d\n",cnt);
} }
|