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
With the deep search, the problem as long as the coordinates are 1, the upper, lower, left, and right are scanned to. Set the coordinates 1 to 0 as long as you've already judged.
#include <iostream> #include <stdio.h> #include <string.h>using namespace Std;int a[100][100],m,n; int b[4][2]={0,1,0,-1,-1,0,1,0};void dfs (int c,int d) {a[c][d]=0;int i,x,y;for (i=0;i<4;i++) {x=c+b[i][0];y=d+b[i][ 1];if (x>=0&&x<m&&y>=0&&y<n&&a[x][y]==1) DFS (x, y);}} int main () {int T,t,i,j;cin>>t;while (t--) {cin>>m>>n;t=0;for (i=0;i<m;i++) for (j=0;j<n;j++) Cin>>a[i][j];for (i=0;i<m;i++) {for (j=0;j<n;j++) {if (a[i][j]==1) {DFS (i,j); t++;}}} Cout<<t<<endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Number of pools