Number of pools

Source: Internet
Author: User

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
Source
[Zhang Yunzun] Original
Uploaded by

Zhang Yunzun

Idea: Reference to the blog: http://blog.csdn.net/a_eagle/article/details/7253336

#include <iostream> #include <string.h>using namespace Std;int a[101][101];int x[4]={0,1,0,-1};int y[4]={ 1,0,-1,0};void BFS (int i,int j) {int k,s,t;   a[i][j]=0;   for (k=0;k<4;k++)   {   s=i+x[k];   T=J+Y[K];   if (a[s][t]==1)   {   BFS (s,t);}}   } int main () {int T,n,m,i,j,count;cin>>t;while (t--) {Count=0;memset (a,0,sizeof (a));cin>>n>>m;       for (i=1;i<=n;i++) for (   j=1;j<=m;j++)   cin>>a[i][j];for (i=1;i<=n;i++) {for (j=1;j<=m;j+ +) {if (a[i][j]==1) {                   BFS (i,j);   count++;}}} Cout<<count<<endl;} return 0;}

Number of pools

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.