SDUT 2152 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大学生程序设计竞赛

示例程序




#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[110][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;
}
 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值