zoukankan      html  css  js  c++  java
  • 2017"百度之星"程序设计大赛

    度度熊的01世界

     
     Accepts: 627
     
     Submissions: 2714
     Time Limit: 2000/1000 MS (Java/Others)
     
     Memory Limit: 32768/32768 K (Java/Others)
    Problem Description

    度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成。

    现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者两者均不是。

    图像0的定义:存在1字符且1字符只能是由一个连通块组成,存在且仅存在一个由0字符组成的连通块完全被1所包围。

    图像1的定义:存在1字符且1字符只能是由一个连通块组成,不存在任何0字符组成的连通块被1所完全包围。

    连通的含义是,只要连续两个方块有公共边,就看做是连通。

    完全包围的意思是,该连通块不与边界相接触。

    Input

    本题包含若干组测试数据。 每组测试数据包含: 第一行两个整数n,m表示图像的长与宽。 接下来n行m列将会是只有01组成的字符画。

    满足1<=n,m<=100

    Output

    如果这个图是1的话,输出1;如果是0的话,输出0,都不是输出-1。

    Sample Input
    32 32
    00000000000000000000000000000000
    00000000000111111110000000000000
    00000000001111111111100000000000
    00000000001111111111110000000000
    00000000011111111111111000000000
    00000000011111100011111000000000
    00000000111110000001111000000000
    00000000111110000001111100000000
    00000000111110000000111110000000
    00000001111110000000111110000000
    00000001111110000000011111000000
    00000001111110000000001111000000
    00000001111110000000001111100000
    00000001111100000000001111000000
    00000001111000000000001111000000
    00000001111000000000001111000000
    00000001111000000000000111000000
    00000000111100000000000111000000
    00000000111100000000000111000000
    00000000111100000000000111000000
    00000001111000000000011110000000
    00000001111000000000011110000000
    00000000111000000000011110000000
    00000000111110000011111110000000
    00000000111110001111111100000000
    00000000111111111111111000000000
    00000000011111111111111000000000
    00000000111111111111100000000000
    00000000011111111111000000000000
    00000000001111111000000000000000
    00000000001111100000000000000000
    00000000000000000000000000000000
    32 32
    00000000000000000000000000000000
    00000000000000001111110000000000
    00000000000000001111111000000000
    00000000000000011111111000000000
    00000000000000111111111000000000
    00000000000000011111111000000000
    00000000000000011111111000000000
    00000000000000111111110000000000
    00000000000000111111100000000000
    00000000000001111111100000000000
    00000000000001111111110000000000
    00000000000001111111110000000000
    00000000000001111111100000000000
    00000000000011111110000000000000
    00000000011111111110000000000000
    00000001111111111111000000000000
    00000011111111111111000000000000
    00000011111111111111000000000000
    00000011111111111110000000000000
    00000000001111111111000000000000
    00000000000000111111000000000000
    00000000000001111111000000000000
    00000000000111111110000000000000
    00000000000011111111000000000000
    00000000000011111111000000000000
    00000000000011111111100000000000
    00000000000011111111100000000000
    00000000000000111111110000000000
    00000000000000001111111111000000
    00000000000000001111111111000000
    00000000000000000111111111000000
    00000000000000000000000000000000
    3 3
    101
    101
    011
    
    Sample Output
    0
    1
    -1
    bfs判断连通包含数量即可
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <map>
    #define ms(x,y) memset(x,y,sizeof(x))
    using namespace std;
    typedef long long ll;
    const int maxn = 1e9 + 2;
    const double pi = acos(-1.0);
    
    const int dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 };
    
    struct node {
        int x, y;
        node(int p, int q)
        {
            x = p;
            y = q;
        }
    };
    
    int n, m;
    char a[110][110] = { 0 };
    bool book[110][110] = { 0 };
    
    int bfs(int x, int y, char c)
    {
        int bao = 1;
        queue<node> que;
        que.push(node(x, y));
        book[x][y] = 1;
        while (que.size())
        {
            node p = que.front(); que.pop();
            for (int i = 0; i < 4; i++)
            {
                int px, py;
                px = p.x + dx[i];
                py = p.y + dy[i];
                if (px >= 0 && px < n&&py >= 0 && py < m)
                {
                    if (!book[px][py] && a[px][py] == c)
                    {
                        book[px][py] = 1;
                        que.push(node(px, py));
                    }
                }
                else
                {
                    bao = 0;
                }
            }
        }
        return bao;
    }
    
    int main()
    {
        while (~scanf("%d%d", &n, &m))
        {
            ms(book, 0);
            ms(a, 0);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    scanf(" %c", &a[i][j]);
                }
            }
    
            int num0 = 0, num1 = 0;
            int bao1 = 0, bao0 = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (!book[i][j])
                    {
                        if (a[i][j] == '0')
                        {
                            num0++;
                            bao1 += bfs(i, j, '0');
                        }
                        else
                        {
                            num1++;
                            bao0 += bfs(i, j, '1');
                        }
                    }
                }
            }
            //printf("num0:%d num1:%d bao0:%d bao1:%d
    ", num0, num1, bao0, bao1);
            if (num1 == 1 && bao1 == 0)
            {
                printf("1
    ");
            }
            else if (num1 == 1 && bao1 == 1)
            {
                printf("0
    ");
            }
            else printf("-1
    ");
        }
    }







    Fighting~
  • 相关阅读:
    跨浏览器OCX
    安装QT5.02
    Ubuntu登陆密码忘记
    QTableView
    VMware安装时Error 1324. The path My Documents contains a invalid character的原因和解决方法
    VS2005 与虚拟机的那点事
    创建掩码位图来实现透明绘图
    【转载】spring注解整理
    记录spring test类无法插入数据问题
    Uniapp 原生开发uniapp.arr 新老兼容问题
  • 原文地址:https://www.cnblogs.com/Archger/p/8451591.html
Copyright © 2011-2022 走看看