度度熊的01世界
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
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
分析:如果0连通块有2块,1连通块有1块,则输出0;如果0,1连通块均为1块,则输出1;否则输出-1。
难点:如何求连通块个数?《算法竞赛入门经典(第二版)》p162页有其讲解用DFS求连通块,俗称种子填充。
#include<stdio.h>
#include<string.h>
#include<math.h>
const int maxn = 110;
int n, m;
char pic[maxn][maxn];
int vis[maxn][maxn];
void dfs(int x, int y, int num, int id)
{
if (x < 0 || x >= n || y < 0 || y >= m)
return;
if (vis[x][y] > 0 || (pic[x][y] - '0') != num)return;
vis[x][y] = id;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (abs(i + j) == 1 )
dfs(x + i, y + j, num, id);
}
}
}
int main()
{
int i, j;
while (scanf("%d%d", &n, &m) == 2)
{
memset(pic, 0, sizeof(pic));
memset(vis, 0, sizeof(vis));
for (i = 0; i < n; i++)
{
getchar();
scanf("%s", pic[i]);
}
int cnt0 = 0, cnt1 = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (pic[i][j] == '0' && !vis[i][j])
dfs(i, j, 0, ++cnt0);
}
}
memset(vis, 0, sizeof(vis));
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (pic[i][j] == '1' && !vis[i][j])
dfs(i, j, 1, ++cnt1);
}
}
if (cnt0 == 2 && cnt1 == 1)printf("0\n");
else if (cnt0 == 1 && cnt1 == 1)printf("1\n");
else printf("-1\n");
}
return 0;
}