Number of nyoj27 pools, nyoj27 pool
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 m (0 <m <100) and number of columns n (0 <n <100) of the map are input first. Then, enter n numbers per line in the next m line, indicating whether there is water or no water here (1 indicates the pool, 0 indicates the ground)
-
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
-
Train of Thought: This question is also a classic topic in deep search. It is similar to the problem of water wa in eight connections. It searches, sums, and starts from each vertex that meets the conditions.
-
The code for this question is as follows:
-
# Include <stdio. h> # include <string. h> int s [101] [101], n, m; void dfs (int I, int j) {if (I <0 | I> m | j <0 | j> n | s [I] [j] = 0) // when all vertices are 0, it indicates that this is a pool return; s [I] [j] = 0; // after each search for a vertex, it is set to 0, avoid repetition // extend dfs (I, j + 1); dfs (I, J-1); dfs (I-1, j); dfs (I + 1, j) ;}int main () {int N; scanf ("% d", & N); while (N --) {int I, j, ans = 0; memset (s, 0, sizeof (s); // initialization, 0 indicates ground, 1 indicates scanf ("% d", & m, & n ); for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) scanf ("% d ", & s [I] [j]) ;}for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) {if (s [I] [j] = 1) // The deep search starts from the place where the pool is located. {dfs (I, j); ans ++; // after the search is complete, both meet the conditions }}printf ("% d \ n", ans); // output result} return 0 ;}