山东省ACM竞赛(2010)---Balloons(BFS)

Balloons

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Both Saya and Kudo like balloons. One day, they heard that in the central park, there will be 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 are too many balloons, it is very hard for them to compute anything they need. Can you help them?
You can assume that the image is an N*N matrix, while each element can be either balloons or blank.
Suppose element and element B are both balloons. They are connected if:
i) They are adjacent;
ii) There is a list of element C1C2, … , Cn, while A and C1 are connected, C1 and C2 are connected …Cn and B are connected.
And a connected block means that every pair of elements in the block is connected, while any element in the block is not connected 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, element A(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?

输入

The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N100), 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 only consists of 0 and 1, while 0 represents a block and 1represents balloons.
The last case is followed by a line containing one zero.

输出

 For 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.

示例输入

5
11001
00100
11111
11010
10010

0

示例输出

Case 1: 3 2

提示

 

来源

 2010年山东省第一届ACM大学生程序设计竞赛




AC Code:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#define size 1001
using namespace std;
int map[102][102];
char a[102][102];
int dis1[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int dis2[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{-1,1},{1,-1}};
int num1,num2;

int BFS1(int x,int y)
{
    if(a[x][y]=='0'||map[x][y]!=0)
        return 0;
    if(a[x][y]=='1')
    {
        map[x][y]=1;
        for(int i=0;i<4;i++)
        BFS1(x+dis1[i][0],y+dis1[i][1]);
        return 1;
    }
    return 0;
}

int BFS2(int x,int y)
{
    if(a[x][y]=='0'||map[x][y]!=0)
        return 0;
    if(a[x][y]=='1')
    {
        map[x][y]=1;
        for(int i=0;i<8;i++)
        BFS2(x+dis2[i][0],y+dis2[i][1]);
        return 1;
    }
    return 0;
}
int main()
{
    int n;
    int s=1;
    while(cin>>n&&n!=0)
    {
        num1=num2=0;
        int i,j;
        for(i=0;i<=n+1;i++)
            a[0][i]=a[n+1][i]=0;
        for(i=0;i<=n+1;i++)
            a[i][0]=a[n+1][0]=0;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            cin>>a[i][j];
        memset(map,0,sizeof(map));
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                num1+=BFS1(i,j);
        memset(map,0,sizeof(map));
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                num2+=BFS2(i,j);
        cout<<"Case "<<s<<": "<<num1<<" "<<num2<<endl<<endl;
        s++;
    }
    return 0;
}


你可能感兴趣的:(C++)