Sdut 2152 Balloons (BFS first Shandong Provincial race)

Source: Internet
Author: User

Balloons Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Title Description

Both Saya and kudo like balloons. One day, they heard, and the Central Park, there'll be a thousands of people fly balloons to pattern a big image.
They were very interested about this event, and also curious about the image.
Since There is too many balloons, it's very hard for them to compute anything they need. Can you help them?
You can assume that the image was anN*nMatrix, while all element can be either balloons or blank.
Suppose elementAand elementBis both balloons. They is connected if:
i) they is adjacent;
II) There is a list of elementC1, C2,. ..,CN, whileA andC1is connected,C1 andC2is connected ...CN andBis connected.
and a connected block means that every pair of elements in the block was connected, while any element in the block was not C Onnected with any element out of the block.
to Saya, element A (Xa,ya)and B (XB,YB) is adjacent if |xa-xb| + |ya-yb|≤1
but to Kudo, elementA (Xa,ya)and element B (XB,YB) is adjacent if |xa-xb|≤1 and |ya-yb|≤1
They want to know that there's how many connected blocks with there own definition of adjacent?

InputThe input consists of several test cases.
The first line of input in each test case contains one integer n (0<n≤), which represents The size of the matrix.
Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string is consists of 0 and 1, while 0 represents a block and 1represents balloons.
The last case was followed by a line containing one zero.OutputFor each case , print the case number (1, 2 ...) and the connected block ' s numbers with Saya and Kudo ' s definition. Your output format should imitate the sample output. Print a blank line after each test case.Sample input
511001001001111111010100100
Sample output
Case 1:3 2
Hint Source 2010 The first ACM University student Program design contest in Shandong Province sample program


#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <    Stdlib.h> #include <queue>using namespace std;struct node{int x; int y;}; int jx1[] = {1,0,0,-1};int jy1[] = {0,1,-1,0};int jx2[] = {1,0,-1,0,-1,1,1,-1};int jy2[] = {0,1,0,-1,-1,1,-1,1};char map[1    10][110];int v[110][110];int n;void BFS1 (int x,int y) {struct node t,f;    queue<node>q1;    T.x = x;    T.y = y;    Q1.push (t);    V[T.X][T.Y] = 1;        while (!q1.empty ()) {t = Q1.front ();        Q1.pop ();            for (int i=0; i<4; i++) {f.x = T.x + jx1[i];            F.Y = T.y + jy1[i]; if (f.x>=0 && f.x<n && f.y>=0 && f.y<n && v[f.x][f.y] = = 0 && map[f.x][                F.Y] = = ' 1 ') {Q1.push (f);            V[F.X][F.Y] = 1;    }}}}void BFS2 (int x,int y) {struct node t,f;    queue<node>q2;    T.x = x;    T.y = y;   Q2.push (t); V[T.X][T.Y] = 1;        while (!q2.empty ()) {t = Q2.front ();        Q2.pop ();            for (int i=0; i<8; i++) {f.x = T.x + jx2[i];            F.Y = T.y + jy2[i]; if (f.x>=0 && f.x<n && f.y>=0 && f.y<n && v[f.x][f.y] = = 0 && map[f.x][                F.Y] = = ' 1 ') {Q2.push (f);            V[F.X][F.Y] = 1;    }}}}int Main () {int KK = 0; while (scanf ("%d", &n)!=eof) {if (n = = 0) {break;}        memset (map,0,sizeof (map));        for (int i=0; i<n; i++) {scanf ("%s", Map[i]);        } memset (V,0,sizeof (v));        int ans = 0;        int cnt = 0;                for (int i=0, i<n; i++) {for (int j=0; j<n; J + +) {if (v[i][j] = = 0)                        {if (map[i][j] = = ' 1 ') {BFS1 (i,j);                    ans++;              }  }}} memset (V,0,sizeof (v));                for (int i=0, i<n; i++) {for (int j=0; j<n; J + +) {if (v[i][j] = = 0)                        {if (map[i][j] = = ' 1 ') {BFS2 (i,j);                    cnt++;        }}}} printf ("Case%d:%d%d\n", ++kk,ans,cnt);    printf ("\ n");  } return 0;}


Sdut 2152 Balloons (BFS first Shandong Provincial race)

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.