hdu 6113 度度熊的01世界

来源:互联网 发布:申请淘宝店铺多少钱 编辑:程序博客网 时间:2024/06/05 00:39

思路:

找到1的连通块个数,之后找0的边界位置。如果没碰到边界,则满足一个被1完全包围,否则未被包围

#include <stdio.h>#include <algorithm>#include <cmath>#include <cstring>#include <queue>#include <iostream>#define maxn 200typedef long long LL;int n, m;char mp[maxn][maxn];bool vis[maxn][maxn];int data[4][2]= {0,1,0,-1,1,0,-1,0};bool dfs(int x,int y){    vis[x][y]=true;    int flag=true;    for(int i=0; i<4; i++)    {        int xx=x+data[i][0];        int yy=y+data[i][1];        if(xx<1||xx>n||yy<1||yy>m)            flag = false;        if(mp[x][y]==mp[xx][yy]&&!vis[xx][yy])            flag &= dfs (xx, yy);    }    return flag;}int main(){    while(~scanf("%d %d", &n, &m))    {        memset(vis, 0, sizeof(vis));        for(int i = 1; i <= n; ++i)            scanf("%s",&mp[i][1]);        int zero = 0, one = 0;        for(int i = 1; i <= n; ++i)            for(int j = 1; j <= m; ++j)            {                if(!vis[i][j])                {                    if(mp[i][j]=='1')                    {                        one++;                        dfs(i,j);                    }                    else                    {                        zero+=dfs(i,j);                    }                }            }        if(one!=1)            printf("-1\n");        else if(zero==0)            printf("1\n");        else if(zero==1)            printf("0\n");        else printf("-1\n");    }    return 0;}
bfs

#include <stdio.h>#include <algorithm>#include <cmath>#include <cstring>#include <queue>using namespace std;int vis[505][505];char mp[505][505];struct node{    int x,y;    node(int _x,int _y){    x=_x,y=_y;}};int zero;int n,m;int data[4][2]={0,1,0,-1,1,0,-1,0};int tot;void bfs(int x,int y,char flag){    queue<node>q;    q.push(node(x,y));    int num=0;    vis[x][y]=1;    while(!q.empty())    {        node tmp =q.front();        q.pop();        for(int i=0;i<4;i++)        {            int xx=tmp.x+data[i][0];            int yy=tmp.y+data[i][1];            if(xx<1||xx>n||yy<1||yy>m)            {                num=1;                continue;            }            if(mp[xx][yy]==mp[x][y]&&!vis[xx][yy])            {                vis[xx][yy]=1;                q.push(node (xx,yy));            }        }    }    //printf(" num %d   flag %d\n",num,flag);    if(!num&&flag=='0')        zero++;}int main(){    int ans=0;    int t;    while(~scanf("%d%d",&n,&m))    {        tot=0;        memset(mp,0,sizeof(mp));        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)            scanf("%s",&mp[i][1]);        zero=0;        int one=0;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                if(!vis[i][j])                {                    if(mp[i][j]=='1')                    {                        one++;                    }                    bfs(i,j,mp[i][j]);/*                    printf("%d %d\n",i,j);                    printf("%d \n",zero);                    printf("---\n");*/                }            }        }        if(one!=1)            printf("-1\n");        else if(zero==1)            printf("0\n");        else if(zero==0)            printf("1\n");        else printf("-1\n");    }}/*5 6000000011100010100011110000000*/



原创粉丝点击