Number of sinks time limit: theMs | Memory Limit:65535KB 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, representing a total of n sets of test data
Each group of data is entered first the number of rows of the map m (0<m<100) and the number of columns N (0<n<100), and then enter the next m row each row input n number, indicating whether there is water or no water (1 means this is the pool, 0 means the ground)
-
-
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
-
-
Idea: The subject is also a deep search of the classic topic for the region, and eight connected to the problem similar to the puddle, from each meet the condition of the vertex began deep search, sum.
-
-
The subject code 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 points are 0 indicate that this is a pool return; s[i][j]=0;//each search for a point, set to 0, to avoid duplication//from this point on the perimeter to 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));//Initialize, 0 for ground, 1 for sink scanf ("%d%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)//Every time from the place where the sink is a deep search { DFS (I,J); ans++; After the search is over, both satisfy the condition}}} printf ("%d\n", ans);//output result}return 0;}
Number of nyoj27 pools