Hdu6113度度熊的01世界(2017"百度之星"程序设计大赛

来源:互联网 发布:麒麟啤酒 中国 知乎 编辑:程序博客网 时间:2024/06/05 17:18

度度熊的01世界

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 674    Accepted Submission(s): 213


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 32000000000000000000000000000000000000000000011111111000000000000000000000001111111111100000000000000000000011111111111100000000000000000001111111111111100000000000000000011111100011111000000000000000001111100000011110000000000000000011111000000111110000000000000000111110000000111110000000000000011111100000001111100000000000000111111000000001111100000000000001111110000000001111000000000000011111100000000011111000000000000111110000000000111100000000000001111000000000001111000000000000011110000000000011110000000000000111100000000000011100000000000000111100000000000111000000000000001111000000000001110000000000000011110000000000011100000000000001111000000000011110000000000000011110000000000111100000000000000011100000000001111000000000000000111110000011111110000000000000001111100011111111000000000000000011111111111111100000000000000000011111111111111000000000000000001111111111111000000000000000000001111111111100000000000000000000001111111000000000000000000000000011111000000000000000000000000000000000000000000000000032 3200000000000000000000000000000000000000000000000011111100000000000000000000000000111111100000000000000000000000011111111000000000000000000000001111111110000000000000000000000001111111100000000000000000000000011111111000000000000000000000001111111100000000000000000000000011111110000000000000000000000001111111100000000000000000000000011111111100000000000000000000000111111111000000000000000000000001111111100000000000000000000000111111100000000000000000000001111111111000000000000000000001111111111111000000000000000000111111111111110000000000000000001111111111111100000000000000000011111111111110000000000000000000000011111111110000000000000000000000000011111100000000000000000000000001111111000000000000000000000001111111100000000000000000000000001111111100000000000000000000000011111111000000000000000000000000111111111000000000000000000000001111111110000000000000000000000000111111110000000000000000000000000011111111110000000000000000000000111111111100000000000000000000000111111111000000000000000000000000000000000000003 3101101011
 

Sample Output
01-1
 

Source
2017"百度之星"程序设计大赛 - 初赛(A)
————————————————————————————————————

dfs,分求01的连通块个数,记得在地图外围加一圈0;


#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <math.h>#include <time.h>#include <algorithm>#include<iostream>#include <complex>#include <vector>#include <stack>#include <queue>#include <unordered_map>#include <set>using namespace std;#define LL long long#define mem(a,b) memset(a,b,sizeof a);const int inf=0x3f3f3f3f;const LL llinf=0x3f3f3f3f3f3f3f3f;const double pi=acos(-1.0);const double eps=1e-8;const LL mod=1e9+7;const int maxn=100005;char mp[105][105];int m, n;int dir[4][2] = {  { -1, 0 }, { 0, -1 }, { 0, 1 },  { 1, 0 }};void dfs(int si, int sj,int fl){    if (si < 0 || sj < 0 || si >m+1 || sj > n+1)        return;    for (int i = 0; i < 4; i++)    {        if (mp[si + dir[i][0]][sj + dir[i][1]] == '0'+fl)        {            mp[si + dir[i][0]][sj + dir[i][1]] = '2';            dfs(si + dir[i][0], sj + dir[i][1],fl);        }    }    return;}int main(){    while (cin >> m >> n)    {        for (int i = 1; i <= m; i++)            for (int j = 1; j <= n; j++)                cin >> mp[i][j];        int t1 = 0,t2=0;        for(int i=0; i<=m+1; i++)            mp[i][0]=mp[i][n+1]='0';        for(int i=1; i<=n+1; i++)            mp[0][i]=mp[m+1][i]='0';        for (int i = 0; i <= m+1; i++)            for (int j = 0; j <= n+1; j++)            {                if (mp[i][j] =='0')                {                    mp[i][j] = '2';                    t1++;                    dfs(i, j,0);                }                if(mp[i][j]=='1')                {                    mp[i][j]='2';                    t2++;                    dfs(i,j,1);                }            }        if(t1==2&&t2==1)            printf("0\n");        else if(t1==1&&t2==1)            printf("1\n");        else            printf("-1\n");    }    return 0;}


原创粉丝点击