Http://poj.org/problem? Id = 1562
Question.
Code:
# Include <cstdio>
# Include <cstring>
Int Tur [8] [2] = {-1,-1,-1, 0,-1, 1, 1,-1, 1, 0, 1, 1, 0, 1, 0,-1 };
Char map [101] [101];
Int n, m, ANS, num;
Bool vis [101] [101];
Struct node {
Int X, Y;
} COOR [1, 10005];
Bool inmap (node p ){
If (p. x> = 0 & P. x <n & P. Y> = 0 & P. Y <m) return true;
Return false;
}
Void BFS (int x, int y ){
Node Q [2, 10005];
Vis [x] [Y] = false;
Ans ++;
Int h = 0;
Int e = 0;
Q [e]. x = X;
Q [E ++]. Y = y;
While (H <E ){
Node P = Q [H ++];
For (INT I = 0; I <8; I ++ ){
Node temp;
Temp. x = p. x + Tur [I] [0];
Temp. Y = P. Y + Tur [I] [1];
If (inmap (temp ))
If (vis [temp. x] [temp. Y]) {
Q [E ++] = temp;
Vis [temp. x] [temp. Y] = false;
}
}
}
}
Int main (){
Int I, J;
While (~ Scanf ("% d", & N, & M) & N + M ){
Num = 0;
Memset (VIS, false, sizeof (VIS ));
For (I = 0; I <n; I ++ ){
Getchar ();
Scanf ("% s", map [I]);
For (j = 0; j <m; j ++ ){
If (Map [I] [J] = '@'){
COOR [num]. x = I;
COOR [num ++]. Y = J;
Vis [I] [J] = true;
}
}
}
Ans = 0;
For (I = 0; I <num; I ++ ){
If (vis [COOR [I]. x] [COOR [I]. Y])
BFS (COOR [I]. X, COOR [I]. y );
}
Printf ("% d \ n", ANS );
}
Return 0 ;}