FIG outermost sheath layer 0 (to prevent the head)
Then the number of blocks in the search graph
'0' has two 0, a 1
'1' has a 0, a 1
The rest does not exist
1 #include<stdio.h> 2 int n,m,z[2],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; 3 char s[102][103]; 4 void dfs(int x,int y){ 5 if(x<0||x>=n||y<0||y>=m) 6 return; 7 char c=s[x][y]; 8 s[x][y]='.'; 9 for(int i=0;i<4;i++){ 10 int xx=x+dx[i],yy=y+dy[i]; 11 if(s[xx][yy]==c) 12 dfs(xx,yy); 13 } 14 } 15 int main(){ 16 int i,j; 17 while(~scanf("%d%d",&n,&m)){ 18 n+=2;m+=2; 19 for(i=0;i<m;i++) 20 s[0][i]=s[n-1][i]='0'; 21 for(i=1;i<n-1;i++){ 22 scanf("%s",s[i]+1); 23 s[i][0]=s[i][m-1]='0'; 24 } 25 for(z[0]=z[1]=i=0;i<n;i++) 26 for(j=0;j<m;j++) 27 if(s[i][j]!='.'){ 28 z[s[i][j]-'0']++; 29 dfs(i,j); 30 } 31 if(z[0]==1&&z[1]==1) 32 puts("1"); 33 else if(z[0]==2&&z[1]==1) 34 puts("0"); 35 else 36 puts("-1"); 37 } 38 39 return 0; 40 }