Pool quantity (nyoj27) (BFS)
Pool quantity time limit: 3000 MS | memory limit: 65535 KB difficulty: 4
-
Description
-
There are some small rivers and some lakes on the campus of Nanyang Institute of Technology. Now we take them all as pools. Suppose there is a map somewhere in our school, this map only identifies whether it is a pool. Now, your task is coming. Please use a computer to figure out several pools in the map.
-
Input
-
Enter an integer N in the first line, indicating that there are N groups of test data.
For each set of data, the number of rows of the map is first input m (0 Output
-
Output The number of pools in the map.
Note that, if the pool is still located next to each pool (up or down the four locations), they can be seen as the same pool.
-
Sample Input
-
23 41 0 0 0 0 0 1 11 1 1 05 51 1 1 1 00 0 1 0 10 0 0 0 01 1 1 0 00 0 1 1 1
-
Sample output
-
23
-
Source
-
[Zhang yuncong] original
-
Uploaded
Zhang yuncong
# Include
# Include
Int s [105] [105]; int a [4] = {0, 0, 1,-1}; // search in four directions. Int B [4] = {1,-1, 0}; void bfs (int x, int y) {int k, v, t; s [x] [y] = 0; for (k = 0; k <4; k ++) {v = x + a [k]; t = y + B [k]; if (s [v] [t]) {bfs (v, t) ;}} int main () {int test, n, m, I, j, ans; scanf ("% d", & test); while (test --) {scanf ("% d", & m, & n); memset (s, 0, sizeof (s); for (I = 1; I <= m; I ++) {for (j = 1; j <= n; j ++) scanf ("% d", & s [I] [j]);} for (I = 1, ans = 0; I <= m; I ++) {for (j = 1; j <= n; j ++) if (s [I] [j]) {bfs (I, j); ans ++;} printf ("% d \ n", ans);} return 0 ;}