度度熊的01世界
Accepts: 967
Submissions: 3064
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
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
Copy
01-1 dfs找连通块。1的连通块有且只有一个,0的可以不存在,如果有只存在一个,否则-1,记录下连通块的个数。 0必须被1包围,因为矩阵中只存在0和1,所以可以理解成只要不到达边界处的0即满足条件。
#include#include char a[105][105],b[105][105];int cc,ff,n,m;void dfs(int x,int y){ if(x<0||y<0||x>=n||y>=m) return; if(a[x][y]=='0'||b[x][y]==1) return; if(a[x][y]=='1'){ b[x][y]=1; dfs(x+1,y); dfs(x,y+1); dfs(x-1,y); dfs(x,y-1); }}void dfss(int x,int y){ if(x<0||y<0||x>=n||y>=m){ ff=1; return; } if(a[x][y]=='1'||b[x][y]==1) return; if(a[x][y]=='0'){ b[x][y]=1; dfss(x+1,y); dfss(x,y+1); dfss(x-1,y); dfss(x,y-1); }}int main(){ int c1,c2,co,f,i,j; while(~scanf("%d%d",&n,&m)){ memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(i=0;i