zoukankan      html  css  js  c++  java
  • 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
    
    ", ++num, sums, sumk);
        }
        return 0;
    }
  • 相关阅读:
    运算符
    javaScript注意事项
    初识JavaScript.
    tomee.xml
    setenv.bat
    catalina.properties
    tomee 系列问题
    tomee 第一个 远程调用 Message-driven bean(MDB)
    tomee 第一个 远程调用 Session Bean
    Java EE 参考文档及sample
  • 原文地址:https://www.cnblogs.com/RootVount/p/11409285.html
Copyright © 2011-2022 走看看