度度熊的01世界

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 874    Accepted Submission(s): 299

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
 

Source

 
 //2017-08-12
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int N = ;
char G[N][N];
int n, m;
int dx[] = {, , , -};
int dy[] = {, , -, };
bool vis[N][N];
struct Node{
int x, y;
Node(int _x, int _y):x(_x), y(_y){}
}; void bfs(int x, int y, int op){
Node node(x, y);
memset(vis, , sizeof(vis));
vis[x][y] = op+;
G[x][y] = '#';
queue<Node> q;
q.push(node);
while(!q.empty()){
Node a = q.front();
q.pop();
for(int i = ; i < ; i++){
int nx = a.x + dx[i];
int ny = a.y + dy[i];
if(nx>= && nx < n && ny>= && ny < m && !vis[nx][ny] && G[nx][ny] == ''+op){
Node tmp(nx, ny);
q.push(tmp);
vis[nx][ny] = op+;
G[nx][ny] = '#';
}
}
}
} int main()
{
//freopen("data1006.txt", "r", stdin);
while(scanf("%d%d", &n, &m)!=EOF){
for(int i = ; i < n; i++){
scanf("%s", G[i]);
}
for(int i = ; i < m; i++){
if(G[][i] == '')bfs(, i, );
if(G[n-][i] == '')bfs(n-, i, );
}
for(int i = ; i < n; i++){
if(G[i][] == '')bfs(i, , );
if(G[i][m-] == '')bfs(i, m-, );
}
int cnt1 = ;
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
if(G[i][j] == ''){
bfs(i, j, );
cnt1++;
}
}
}
int cnt2 = ;
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
if(G[i][j] == ''){
bfs(i, j, );
cnt2++;
}
}
}
if(cnt1== && cnt2 == )printf("1\n");
else if(cnt1 == && cnt2 == )printf("0\n");
else printf("-1\n");
} return ;
}

最新文章

  1. C#非常重要基础之多态
  2. objective-c第七章课后练习3
  3. I2C总线和S5PV210的I2C总线控制器
  4. 2016HUAS暑假集训训练2 J - 今年暑假不AC
  5. html5 canvas 粒子特效
  6. kettle删除资源库中的转换或者作业
  7. c语言调试开关
  8. HDU1431+简单题
  9. jQuery的选择器中的通配符[id^=&#39;code&#39;]或[name^=&#39;code&#39;]
  10. iOS8中的动态文本
  11. cookie记忆换肤功能实战Demo
  12. PHP利用MySQL保存session
  13. QWaitCondition 的思考2
  14. 金9银10,分享几个重要的Android面试题
  15. 如何给webbrowser指定IE版本
  16. xxx.jar 中没有主清单属性
  17. db2修改最大连接数
  18. if-else和while循环
  19. iOS:CoreText的常用语法
  20. 解析xml文件步骤 -- pullparser

热门文章

  1. Django 实现第三方账号登录网站
  2. apache ab
  3. dubbo实现原理之SPI简介
  4. [vue] [axios] 设置代理实现跨域时的纠错
  5. 无图形界面安装CentOS
  6. async await 的执行
  7. [Umbraco] document type里的父节点与子节点的设置
  8. (转)Python Mixins 机制
  9. Installation failed with message...It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.
  10. 全网最全的Windows下Anaconda2 / Anaconda3里正确下载安装用来定时任务apscheduler库(图文详解)