Number of nyoj27 pools (DFS)

Source: Internet
Author: User

    • The topic of
    • topic information
    • run result
    • line
    • discussion area
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

Go through the map first to find the pool and deep search, the pool next to it all marked, at the same time the number of pools +1

Very simple. But remember this is about four directions up and down. Not eight directions 0.0

#include <stdio.h> #include <string.h>int t,m,n,i,j,count;int visit[100][100],a[100][100];void dfs (int s1 , int s2) {if (s1<0| | s1==m| | s2<0| | s2==n| | visit[s1][s2]| |! A[S1][S2]) return, Visit[s1][s2]=1;dfs (S1+1,S2);d FS (S1-1,S2);d FS (s1,s2-1);d FS (s1,s2+1);} int main () {scanf ("%d", &t), while (t--) {scanf ("%d%d", &m,&n), memset (A,0,sizeof (a)); Memset (visit,0, sizeof (visit)); for (i=0;i<m;i++) for (j=0;j<n;j++) scanf ("%d", &a[i][j]); Count=0;for (i=0;i<m;i++) for (j =0;j<n;j++) {if (a[i][j]==1&&visit[i][j]==0) Count++,dfs (i,j);} printf ("%d\n", Count);}}        


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Number of nyoj27 pools (DFS)

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.