http://acm.hdu.edu.cn/showproblem.php?pid=6113
【题意】
度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由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
Copy
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
Copy
0 1 -1【思路】给每个图外面加上一圈0,避免判断边界的情况,然后BFS或DFS求连通块的个数如果是0,则0的连通块个数恰为2,1的连通块个数恰为1如果是1,则0的连通块个数恰为1,1的连通块个数恰为1否则不是0也不是1【AC】
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 typedef long long ll; 10 const int maxn=1e2+3; 11 int n,m; 12 char s[maxn][maxn]; 13 bool vis[maxn][maxn]; 14 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 15 struct node 16 { 17 int x; 18 int y; 19 node(){} 20 node(int _x,int _y):x(_x),y(_y){} 21 }; 22 void bfs(int x,int y,char ch) 23 { 24 queue<node> Q; 25 Q.push(node(x,y)); 26 vis[x][y]=true; 27 while(!Q.empty()) 28 { 29 node q=Q.front(); 30 Q.pop(); 31 for(int i=0;i<4;i++) 32 { 33 x=q.x+dir[i][0]; 34 y=q.y+dir[i][1]; 35 if(x>=0&&x<=n+1&&y>=0&&y<=m+1&&!vis[x][y]&&s[x][y]==ch) 36 { 37 vis[x][y]=true; 38 Q.push(node(x,y)); 39 } 40 } 41 } 42 43 } 44 int main() 45 { 46 while(~scanf("%d%d",&n,&m)) 47 { 48 memset(vis,false,sizeof(vis)); 49 for(int i=1;i<=n;i++) 50 { 51 scanf("%s",s[i]+1); 52 } 53 for(int i=0;i<=n+1;i++) 54 { 55 s[i][0]=s[i][m+1]=‘0‘; 56 } 57 for(int i=0;i<=m+1;i++) 58 { 59 s[0][i]=s[n+1][i]=‘0‘; 60 } 61 int x0=0,x1=0; 62 for(int i=0;i<=n+1;i++) 63 { 64 for(int j=0;j<=m+1;j++) 65 { 66 if(!vis[i][j]) 67 { 68 if(s[i][j]==‘1‘) 69 { 70 x1++; 71 bfs(i,j,‘1‘); 72 } 73 else 74 { 75 x0++; 76 bfs(i,j,‘0‘); 77 } 78 } 79 } 80 } 81 if(x0==2&&x1==1) 82 { 83 puts("0"); 84 } 85 else if(x0==1&&x1==1) 86 { 87 puts("1"); 88 } 89 else 90 { 91 puts("-1"); 92 } 93 } 94 return 0; 95 }
外面加一圈0简化问题
【注意】
这样是不对的,后面输入的s最后一个字符是‘\0‘,会覆盖一开始赋的值‘0‘
1 for(int i=0;i<=n+1;i++) 2 { 3 s[i][0]=s[i][m+1]=‘0‘; 4 } 5 for(int i=0;i<=m+1;i++) 6 { 7 s[0][i]=s[n+1][i]=‘0‘; 8 } 9 for(int i=1;i<=n;i++) 10 { 11 scanf("%s",s[i]+1); 12 }
错误写法
应该先输入再赋值
度度熊的01世界
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1238 Accepted Submission(s): 445
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
Source