hdu 6113 度度熊的01世界

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
 
思路:dfs搜索即可,在原来的图的基础上四周都再加一层0,这样一来如果整个图形是1,搜索到的0或1的连通块数都只有1块,如果整个图形是0,那么0的连通块数为内外一共2块,1的连通块数为1块,其余情况都为-1.
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
const int N_MAX = 100 + 5;
int n, m;
char field[N_MAX][N_MAX];

int direction[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };

void dfs(const int& x,const int& y,const char& c) {
    field[x][y] = '*';
    for (int i = 0; i < 4; i++) {
        int dx = x + direction[i][0], dy = y + direction[i][1];
        if (dx >= 0 && dx <= n+1&&dy >= 0 && dy <= m+1&&field[dx][dy] == c)dfs(dx, dy, c);
    }
}

int main() {
    while (scanf("%d%d",&n,&m)!=EOF) {
        for (int i = 1; i < n+1;i++) {
            for (int j = 1; j < m+1;j++) {
                 scanf(" %c",&field[i][j]);
            }
        }
        for (int i = 0; i < n + 2; i++)field[i][0] = '0',field[i][m + 1] = '0';
        for (int i = 0; i < m + 2; i++)field[0][i] = '0', field[n + 1][i] = '0';
        int num_0 = 0,num_1=0;
        for (int i = 0; i < n+2; i++) {
            for (int j = 0; j < m+2; j++) {
                if (field[i][j] == '0') {
                    num_0++;
                    dfs(i, j, '0');
                }
                if (field[i][j] == '1') {
                    num_1++;
                    dfs(i,j,'1');
                }
            }
        }
        if (num_0 == 2 && num_1 == 1) printf("0\n");
        else if(num_0==1&&num_1==1)    printf("1\n");
        else printf("-1\n");
    }
    return 0;
}

 

 

posted on 2017-08-12 21:10  ZefengYao  阅读(168)  评论(0编辑  收藏  举报

导航