Number of nyoj 27 pools

Source: Internet
Author: User

 

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. Each group of data first enters the number of rows of the map (0 <m <100) and the number of columns N (0 <n <100). Then, enter n numbers for each row in the next M line, indicating whether there is water or no water (1 indicates that the pool is used here, 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


# Include <stdio. h> # include <string. h> int sum; int M, N; int e [100] [100]; // the two-dimensional array int next [4] [2] =,, 0,-1,-}; // defines a direction array void DFS (int I, Int J) // Deep Search {int X, Y; E [I] [J] = 0; // assign all connected pools 0; For (int K = 0; k <4; k ++) {x = I + next [k] [0]; y = J + next [k] [1]; if (x <0 | x> M-1 | Y <0 | Y> N-1) // you can determine whether to cross-border continue; else if (E [x] [Y] = 1) {DFS (x, y) ;}} return ;}int main () {int num; scanf ("% d", & num); While (Num --) {sum = 0; scanf ("% d", & M, & N ); for (INT I = 0; I <m; I ++) {for (Int J = 0; j <n; j ++) {scanf ("% d ", & E [I] [J]) ;}}for (INT I = 0; I <m; I ++) {for (Int J = 0; j <N; j ++) {If (E [I] [J] = 1) {sum ++; DFS (I, j );}}} printf ("% d \ n", sum);} return 0 ;}

 




 




 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.