Description
度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由
0
和
现在给你一个
n∗m
的图像,你需要分辨他究竟是
0
,还是
图像
0
的定义:存在
图像
1
的定义:存在
连通的含义是,只要连续两个方块有公共边,就看做是连通。
完全包围的意思是,该连通块不与边界相接触。
Input
本题包含若干组测试数据。
每组测试数据包含:
第一行两个整数
接下来
n
行
满足 1≤n,m≤100
Output
如果这个图是
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
Solution
简单搜索,
dfs
统计
1
连通块的数量和被
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
#define maxn 111
char s[maxn][maxn];
int n,m,mark[maxn][maxn],L,R,U,D;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
void dfs(int x,int y)
{
if(s[x][y]=='0')U=min(U,x),D=max(D,x),L=min(L,y),R=max(R,y);
mark[x][y]=1;
for(int i=0;i<4;i++)
{
int xx=x+dx[i],yy=y+dy[i];
if(xx<1||xx>n||yy<1||yy>m||mark[xx][yy]||s[xx][yy]!=s[x][y])continue;
dfs(xx,yy);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
memset(mark,0,sizeof(mark));
int num1=0;//1连通块数量
int cnt0=0;//被1包围的0连通块数量
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(!mark[i][j])
{
if(s[i][j]=='1')num1++;
L=m,R=1,U=n,D=1;
dfs(i,j);
if(s[i][j]=='0'&&L>1&&R<m&&U>1&&D<n) cnt0++;
}
if(num1==1&&cnt0==1)printf("0\n");
else if(num1==1&&cnt0==0)printf("1\n");
else printf("-1\n");
}
return 0;
}