HDU-6113

度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由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


题解:(DFS或BFS都可以,下面为DFS)这道题我们可以先将所有的数一周假一圈0,然后查找1和0区块的数量;当1区块数量为1个,且0区块数量为2个时,必定有一个0区块被1区块包围;当1区块为1个,0区块也为1个时,没有0区块被包围;其他情况为-1.

AC代码为:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;


int vis[110][110], n, m;
char str[110][110];
int dfsx[4] = { 1,-1,0,0 };
int dfsy[4] = { 0,0,1,-1 };


void dfs(int x, int y, char ch)
{
for (int i = 0; i<4; i++)
{
int tx = x + dfsx[i];
int ty = y + dfsy[i];
if (tx<0 || tx>n + 1 || ty<0 || ty>m + 1)
continue;
if (vis[tx][ty])
continue;
if (str[tx][ty] == ch)
{
vis[tx][ty] = 1;
dfs(tx, ty, ch);
}
}
}


int main()
{
int sum0, sum1;
while (~scanf("%d%d", &n, &m))
{
sum0 = 0;
sum1 = 0;


for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
cin >> str[i][j];
}


for (int i = 0; i <= n + 1; i++)
{
str[i][0] = '0';
str[i][m + 1] = '0';
}
for (int i = 0; i <= m + 1; i++)
{
str[0][i] = '0';
str[n + 1][i] = '0';
}


memset(vis, 0, sizeof(vis));
for (int i = 0; i<n + 1; i++)
{
for (int j = 0; j<m + 1; j++)
{
if (!vis[i][j])
{
if (str[i][j] == '1')
sum1++;
if (str[i][j] == '0')
sum0++;
vis[i][j] = 1;
dfs(i, j, str[i][j]);
}
}
}


if (sum0 == 1 && sum1 == 1)
cout << 1 << endl;
else if (sum0 == 2 && sum1 == 1)
cout << 0 << endl;
else
cout << -1 << endl;
}


return 0;
}








posted @ 2018-03-04 23:00  StarHai  阅读(248)  评论(0编辑  收藏  举报