Number of NYOJ27 pools

Source: Internet
Author: User

Number of pools

Time limit: ms | Memory limit:65535 KB

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


At first, I didn't think to modify the values on the map, but instead used an array, which is more complex than the standard path.

#include  <iostream>using namespace std;int m1,m2;int dp[100][100],map[100][100] ;//,DIRECT[4][2];INT&NBSP;DIRECT[4][2]={{1,0},{0,1},{-1,0},{0,-1}};VOID&NBSP;DP (int x, int y) { Int k,xx,yy;dp[x][y]=1;for (k=0;k<4;k++) {xx=x+direct[k][0];yy=y+direct[k][1];if (xx>=0&&xx <m1 && yy>=0&&yy<m2 && 1==map[xx][yy] && &NBSP;0==DP[XX][YY]) DP (XX,YY);}} Int search (int x, int y) {int k,xx,yy;if (1==map[x][y])  &&  (0==dp[x] [y])) {dp[x][y]=1;for (k=0;k<4;k++) {xx=x+direct[k][0];yy=y+direct[k][1];//yy>=0&&yy<m2  was written  yy>0&&yy<m2  wasted more than 2 hours .... if (xx>=0&&xx<m1 && yy>=0&&yy<m2 && 1==map[xx][ YY]&NBSP;&AMP;&AMP;&NBSP;0==DP[XX][YY]) DP (XX,YY);} return 1;} return 0;} Int main () {int n,i,j,count;cin>>n;while (n--) {//Build map cin>>m1>>m2;for (i=0;i<m1;i++) for (j=0;j<m2;j++) {cin>>map[i][j];dp [i][j]=0;} Find the Pool count=0;for (i=0;i<m1;i++) for (j=0;j<m2;j++) if (Search (i,j)) ++count;cout<<count<<endl;} return 0;}


After I've seen the label, I changed it.

#include <iostream>using namespace Std;int map[102][102];int direct[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int M1, M2;void Search (int x, int y) {int k,xx,yy;for (k=0;k<4;k++) {xx=x+direct[k][0];yy=y+direct[k][1];if (xx>=0& &XX<M1 && yy>=0&&yy<m2 && 1==map[xx][yy]) {map[xx][yy]=0; Search (XX,YY);}} int main () {int N,i,j,count;cin>>n;while (n--) {//Build map Cin>>m1>>m2;for (i=0;i<m1;i++) for (j=0;j <m2;j++) cin>>map[i][j];count=0;//Find pool for (i=0;i<m1;i++) for (j=0;j<m2;j++) {if (Map[i][j]) {count++; map[i][j]=0; Search (I,J);}} Cout<<count<<endl;} return 0;}


This article is from the "hacker" blog, make sure to keep this source http://anglecode.blog.51cto.com/5628271/1631173

Number of NYOJ27 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.