度度熊的01世界
Accepts: 967
Submissions: 3064
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
严格按照题目的要求来做这题就过了↓
DFS记录0和1的连通块个数和没有被包围的连通块个数
之后判断一下就好了
#include<stdio.h>
#include<string.h>
int n, m, ok, flag[105][105], a[105][105], dir[4][2] = {1,0,0,1,-1,0,0,-1};
void Sech(int x, int y)
{
int dx, dy, i;
flag[x][y] = 1;
for(i=0;i<=3;i++)
{
dx = x+dir[i][0];
dy = y+dir[i][1];
if(dx<1 || dx>n || dy<1 || dy>m)
{
ok = 0;
continue;
}
if(flag[dx][dy]==1 || a[dx][dy]!=a[x][y])
continue;
Sech(dx, dy);
}
}
int main(void)
{
int i, j, z0, z1, x0, x1;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(flag, 0, sizeof(flag));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
scanf("%1d", &a[i][j]);
}
z0 = z1 = x0 = x1 = 0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(flag[i][j]==0)
{
ok = 1;
Sech(i, j);
if(a[i][j]==0)
z0++, x0 += ok;
else
z1++, x1 += ok;
}
}
}
if(z1==1 && x0==0)
printf("1\n");
else if(z1==1 && x0==1)
printf("0\n");
else
printf("-1\n");
}
return 0;
}