1 // question address: http://acm.nyist.net/JudgeOnline/status.php? PID = 27
2 // solution idea: bare DFS, traversing each lattice DFS to access the phase grid of the current Lattice
3 # include <stdio. h>
4 # include <string. h>
5
6 int map [110] [110], vis [110] [110];
7 int DX [4] = {0, 0,-1, 1 };
8 int dy [4] = {-1, 1, 0, 0 };
9
10 void DFS (int x, int y ){
11 int I;
12 if (Map [x] [Y] = 0 | Vis [x] [Y] = 1) return; // if the pool has been accessed or does not exist, return
13 vis [x] [Y] = 1;
14 For (I = 0; I <4; I ++ ){
15 DFS (x + dx [I], Y + dy [I]);
16}
17 // DFS (X-1, y );
18 // DFS (x + 1, y );
19 // DFS (X, Y-1 );
20 // DFS (X, Y + 1 );
21}
22
23 int main (){
24 int t, n, m, I, J;
25 int ans;
26 scanf ("% d", & T );
27 while (t --){
28 scanf ("% d", & N, & M );
29 memset (MAP, 0, sizeof (MAP ));
30 memset (VIS, 0, sizeof (VIS ));
31 For (I = 1; I <= N; I ++ ){
32 For (j = 1; j <= m; j ++ ){
33 scanf ("% d", & map [I] [J]);
34}
35}
36 ans = 0;
37 For (I = 1; I <= N; I ++ ){
38 for (j = 1; j <= m; j ++ ){
39 if (Map [I] [J] = 1 & Vis [I] [J] = 0 ){
40 ans ++;
41 DFS (I, j );
42}
43}
44}
45 printf ("% d \ n", ANS );
46}
47 return 0;
48}
Nyoj-27-Number of pools