NYOJ --- Question 27 pool quantity, NYOJ---27 pool quantity
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.
For each set of data, the number of rows m (0 <m <100) and number of columns n (0 <n <100) of the map are input first. Then, enter n numbers per line in the next m line, indicating whether there is water or no water here (1 indicates the pool, 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
-
Source
-
[Zhang yuncong] original
-
Uploaded
-
Zhang yuncong
-
Analysis: this is a simple DFS question. Starting from a point, we change all vertices of 1 to 0. After several searches, there are several pools;
The Code is as follows:
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int dx[]={1,0,-1,0};int dy[]={0,1,0,-1};int vis[110][110];int n,m;inline bool in(int x,int y){ if(x>=0&&x<m&&y>=0&&y<n) return true; return false;}void dfs(int x,int y){ vis[x][y]=0; int newx,newy; for(int i=0;i<4;i++) { newx=x+dx[i]; newy=y+dy[i]; if(vis[newx][newy]==1&&in(newx,newy)) { dfs(newx,newy); } }}int main(){ int test; cin>>test; while(test--) { cin>>m>>n; int count=0; for(int i=0;i<m;i++) for(int j=0;j<n;j++) { cin>>vis[i][j]; } for(int i=0;i<m;i++) for(int j=0;j<n;j++) { if(vis[i][j]==1) { dfs(i,j); count++; } } printf("%d\n",count); }}
-