SDNU 1136.Balloons(BFS)

Description

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 A and element B are both balloons. They are connected if:
i) They are adjacent;
ii) There is a list of element C1, C2, … , 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?

Input

The input consists of several test cases.
The first line of input in each test case contains one integer N (0<n≤100), which="" represents="" of="" the="" matrix.<br="" size=""> 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.

Output

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.

Sample Input

5
11001
00100
11111
11010
10010

0

Sample Output

Case 1: 3 2

Source

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define eps 1e-9

const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const int maxn = 100000 + 8;

int mov[8][2] = {0,1, 1,0, 0,-1, -1,0, 1,1, 1,-1, -1,1, -1,-1};

int n;
string s[100000 + 8], ss[100000 + 8];
ll sums, sumk;

struct node
{
    int x, y;
};

void bfs1(int a, int b)
{
    queue<node>q;
    node miao;
    miao.x = a;
    miao.y = b;
    q.push(miao);
    while(!q.empty())
    {
        node f = q.front();
        q.pop();
        node next;
        for(int i = 0; i < 4; i++)
        {
            next.x = f.x + mov[i][0];
            next.y = f.y + mov[i][1];
            if(next.x >= 0 && next.y >= 0 && next.x < n && next.y < n && s[next.x][next.y] == '1')
            {
                s[next.x][next.y] = '0';
                q.push(next);
            }
        }
    }
}

void bfs2(int a, int b)
{
    queue<node>q;
    node miao;
    miao.x = a;
    miao.y = b;
    q.push(miao);
    while(!q.empty())
    {
        node f = q.front();
        q.pop();
        node next;
        for(int i = 0; i < 8; i++)
        {
            next.x = f.x + mov[i][0];
            next.y = f.y + mov[i][1];
            if(next.x >= 0 && next.y >= 0 && next.x < n && next.y < n && ss[next.x][next.y] == '1')
            {
                ss[next.x][next.y] = '0';
                q.push(next);
            }
        }
    }
}

int main()
{
    std::ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int num = 0;
    while(cin>>n)
    {
        if(n == 0)break;
        for(int i = 0; i < n; i++)
        {
            cin>>s[i];
            ss[i] = s[i];
        }
        sums = 0;
        sumk = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(s[i][j] == '1')
                {
                    s[i][j] = '0';
                    bfs1(i, j);
                    sums++;
                }
            }
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(ss[i][j] == '1')
                {
                    ss[i][j] = '0';
                    bfs2(i, j);
                    sumk++;
                }
            }
        }
        printf("Case %d: %lld %lld\n\n", ++num, sums, sumk);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/RootVount/p/11409285.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值