度度熊的01世界

度度熊的01世界

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


0有1个洞
1 没有洞
搜索,根据特征判断

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
char mp[111][111];
struct node
{
    int n, m;
    node() {}
    node(int x, int y) { n = x; m = y; }
}t;
queue<node>q;
int main()
{
    int m, n;
    while (cin >> n >> m)
    {
        int i, j;
        for (i = 0; i <= n + 1; i++)
        {
            mp[i][0] = mp[i][m + 1] = '0';
        }
        for (j = 0; j <= m + 1; j++)
        {
            mp[n + 1][j] = mp[0][j] = '0';
        }
        for (i = 1; i <= n; i++)
        {
            scanf("%s", mp[i] + 1);
            mp[i][m + 1] = '0';
        }
        int one(0), zero(0);
        char sta =0;
        int x, y;
        for (i = 0; i <= n + 1; i++)
        {
            for (j = 0; j <=m + 1; j++)
            {
                if (mp[i][j] != 0)
                {
                    if (mp[i][j] == '1')
                    {
                        one++; sta = '1';
                    }
                    else
                    {
                        zero++; sta = '0';
                    }
                    q.push(node(i, j));
                    while (!q.empty())
                    {
                        t = q.front();
                        x = t.n;
                        y = t.m;
                        q.pop();
                        mp[x][y] = 0;
                        if (x - 1 >= 0 && mp[x - 1][y] == sta)
                        {
                            q.push(node(x - 1, y));
                            mp[x - 1][y] = 0;
                        }
                        if (x + 1 <= n + 1 && mp[x + 1][y] == sta)
                        {
                            q.push(node(x + 1, y));
                            mp[x + 1][y] = 0;
                        }
                        if (y - 1 >= 0 && mp[x][y - 1] == sta)
                        {
                            q.push(node(x, y - 1)); mp[x][y - 1] = 0;
                        }
                        if (y + 1 <= m + 1 && mp[x][y + 1] == sta)
                        {
                            q.push(node(x, y + 1)); mp[x][y + 1] = 0;
                        }
                    }
                }
                if (one > 1)
                {
                    printf("-1\n");
                    goto end;
                }
                if (zero > 2)
                {
                    printf("-1\n");
                    goto end;
                }
            }
        }
        if (zero == 2 && one == 1)
            printf("0\n");
        else if (zero == 1 && one == 1)
            printf("1\n");
        else
            printf("-1\n");
    end:;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值