百度之星初赛(A)——T6
度度熊的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 Outpu0
1 -1
——————————————————————————————————————————————
这道题在外面加一圈0就好了(这个是防止被一坨1卡的尴尬)
然后就是联通块染色
如果1有一个0两个就是0
如果1有一个0也一个就是1
不然就是-1
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int n,m,cnt1,cnt2; char s[155][155]; bool f,vis[155][155]; int xi[5]={0,0,1,0,-1},yi[5]={0,1,0,-1,0}; struct node{ int x,y; }q[155*155]; void dfs(int xx,int yy,char c){ int head=0,tail=1; q[0].x=xx; q[0].y=yy; vis[xx][yy]=1; while(head!=tail){ int x=q[head].x,y=q[head].y; head++; for(int i=1;i<=4;i++){ int sx=x+xi[i],sy=y+yi[i]; if(sx>=0&&sx<=n+1&&sy>=0&&sy<=m+1&&s[sx][sy]==c&&!vis[sx][sy]){ q[tail].x=sx; q[tail].y=sy; tail++; vis[sx][sy]=1; } } } } int main() { while(scanf("%d %d",&n,&m)==2){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); cnt1=0; cnt2=0; for(int i=0;i<=m+1;i++) s[0][i]='0',s[n+1][i]='0'; for(int i=0;i<=n+1;i++) s[i][0]='0',s[i][m+1]='0'; for(int i=0;i<=n+1;i++) for(int j=0;j<=m+1;j++)if(!vis[i][j]){ if(s[i][j]=='1') cnt1++; else cnt2++; dfs(i,j,s[i][j]); } if(cnt1==1&&cnt2==2) printf("0\n"); else if(cnt1==1&&cnt2==1) printf("1\n"); else printf("-1\n"); } return 0; }