Number of sinks time limit:MS | Memory limit:65535 KB Difficulty:4
-
Describe
-
Nanyang Polytechnic Campus There are some rivers and some lakes, now, we think of them as a pool, suppose there is a map of our school, this map only to identify whether this is a pool, now, your task came, please use the computer to calculate the total number of pools in the map.
-
-
Input
-
The
-
first line enters an integer n, which represents a total of n sets of test data each set of data is first entered the number of rows of the map m (0<m<100) and number of columns N (0<n<100), and then enter the next m row per row input n number, Indicates if there is water or no water (1 means the pool is here, 0 means the ground is here)
-
-
Output
-
-
outputs the number of pools in the map. It is important to note that each pool side (up and down four positions) can be considered to be the same pool if it is a sink.
-
-
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
1 2#include <iostream>3#include <cstdio>4#include <cstring>5 using namespacestd;6 7 intvisit[ the][ the];8 ints[ the][ the], M, N;9 Const intdx[4] = {-1,0,1,0};Ten Const intdy[4] = {0,1,0,-1}; One A voidDfsintIintj) - { -VISIT[I][J] =1; the for(intK =0; K <4; k++) - { - intNX = i +Dx[k]; - intNY = j +Dy[k]; + if(NX >=0) && (NX < m) && (NY >=0) && (NY < N) && (Visit[nx][ny] = =0) && (S[nx][ny] = =1)) - { + DFS (NX,NY); A } at } - } - - - intMain () { - intcou, test; inCIN >>test; - to while(test--) + { -Cou =0; thememset (Visit,0,sizeof(visit)); *memset (s),0,sizeof(s)); $ Panax NotoginsengCIN >> M >>N; - for(inti =0; I < m; i++) the for(intj =0; J < N; J + +) +CIN >>S[i][j]; A the for(inti =0; I < m; i++) + for(intj =0; J < N; J + +) - if(Visit[i][j] = =0&& S[i][j] = =1) $ { $cou++; - Dfs (I, j); - } the -printf"%d\n", cou);Wuyi } the return 0; -}
View Code
Nyoj 27 Pool Number