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<=100Output如果这个图是1的话,输出1;如果是0的话,输出0,都不是输出-1。Sample Input32 32000000000000000000000000000000000000000000011111111000000000000000000000001111111111100000000000000000000011111111111100000000000000000001111111111111100000000000000000011111100011111000000000000000001111100000011110000000000000000011111000000111110000000000000000111110000000111110000000000000011111100000001111100000000000000111111000000001111100000000000001111110000000001111000000000000011111100000000011111000000000000111110000000000111100000000000001111000000000001111000000000000011110000000000011110000000000000111100000000000011100000000000000111100000000000111000000000000001111000000000001110000000000000011110000000000011100000000000001111000000000011110000000000000011110000000000111100000000000000011100000000001111000000000000000111110000011111110000000000000001111100011111111000000000000000011111111111111100000000000000000011111111111111000000000000000001111111111111000000000000000000001111111111100000000000000000000001111111000000000000000000000000011111000000000000000000000000000000000000000000000000032 3200000000000000000000000000000000000000000000000011111100000000000000000000000000111111100000000000000000000000011111111000000000000000000000001111111110000000000000000000000001111111100000000000000000000000011111111000000000000000000000001111111100000000000000000000000011111110000000000000000000000001111111100000000000000000000000011111111100000000000000000000000111111111000000000000000000000001111111100000000000000000000000111111100000000000000000000001111111111000000000000000000001111111111111000000000000000000111111111111110000000000000000001111111111111100000000000000000011111111111110000000000000000000000011111111110000000000000000000000000011111100000000000000000000000001111111000000000000000000000001111111100000000000000000000000001111111100000000000000000000000011111111000000000000000000000000111111111000000000000000000000001111111110000000000000000000000000111111110000000000000000000000000011111111110000000000000000000000111111111100000000000000000000000111111111000000000000000000000000000000000000003 3101101011Sample Output01-1
题解:搜索先给图像外加一圈0,然后通过搜索记录联通块的个数和种类。若整个图中:仅有两块0和一块1:输出0
仅有一块0和一块1:输出1否则输出-1
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<queue> using namespace std; const int N=110; int n, m, map[N][N], ans[2]; char s[N]; bool vis[N][N]; struct node{ int x, y; node(){} node( int a, int b ) { x=a; y=b; } }; queue< node >que; int to[4][2]={ { 1, 0 }, { -1, 0 }, { 0, -1 }, { 0, 1 } }; bool check( int x, int y, bool flg ) { if( x>=0 && x<=n && y>=0 && y<=m ) { if( !vis[x][y] && map[x][y]==flg ) return 1; } return 0; } void BFS( int x, int y ) { que.push( node( x, y ) ); vis[x][y]=1; while( !que.empty() ) { x=que.front().x; y=que.front().y; que.pop(); for( int i=0; i<4; i++ ) { int xx=x+to[i][0], yy=y+to[i][1]; if( check( xx, yy, map[x][y] ) ) { que.push( node( xx, yy ) ); vis[xx][yy]=1; } } } } int main() { while( ~scanf( "%d%d", &n, &m ) ) { ans[0]=ans[1]=0; memset( vis, 0, sizeof vis ); memset( map, 0, sizeof map ); for( int i=1; i<=n; i++ ) { scanf( "%s", s+1 ); for( int j=1; j<=m; j++ ) map[i][j]=s[j]-'0'; } n++; m++; for( int i=0; i<=n; i++ ) for( int j=0; j<=m; j++ ) if( !vis[i][j] ) { ans[ map[i][j] ]++; BFS( i, j ); } if( ans[1]==1 && ans[0]==1 ) printf( "1\n" ); else if( ans[1]==1 && ans[0]==2 ) printf( "0\n" ); else printf( "-1\n" ); } return 0; }
[HDU6113][2017"百度之星"程序设计大赛 - 初赛(A)]度度熊的01世界
最新推荐文章于 2020-08-25 08:51:57 发布