度度熊的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 ;
}

HDU6113的更多相关文章

  1. 2017百度之星初赛A-1006(HDU-6113)

    思路:在图的外面包一圈'0'字符,然后dfs统计'0'字符的个数和'1'字符的个数.结果如下(num0表示0字符的个数,num1表示1字符的个数): num0 == 1 && num1 ...

  2. HDU-6113

    度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成. 现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者两者均不是. 图像0的定义:存在1字符且1字符只能是由一个 ...

随机推荐

  1. Spring Boot中使用JdbcTemplate访问数据库

    本文介绍在Spring Boot基础下配置数据源和通过JdbcTemplate编写数据访问的示例. 数据源配置 在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式. ...

  2. 前端开发者不得不知的ES6十大特性

    前端开发者不得不知的ES6十大特性 转载 作者:AlloyTeam 链接:http://www.alloyteam.com/2016/03/es6-front-end-developers-will- ...

  3. Failed to start docker.service: Unit not found.

    安装教程参考: https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce-1 https://yq.aliyu ...

  4. flask_json数据入库Mongo

    首先我们先导入python内置的json库,用来将接送数据转换为python对象 import json #导入自定义的数据公共库 from db_tool import db #载入库之前先清空数据 ...

  5. mongodb postgresql mysql jsonb对比

    mongodb pg mysql jsonb对比 http://erthalion.info/2017/12/21/advanced-json-benchmarks/ 使用禁用jsonb列的压缩 AL ...

  6. shopify网站转化率优化之结账页checkout优化

    昨天分享了“利用GOOGLE地图API实现shopify结账页checkout地址自动填写地址字段”是个非常屌的功能,但shopify默认的结账页checkout是这样的如图 [caption id= ...

  7. 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 1、两层神经网络的单样本向量化表示与多样本向量化表示

    如上图所示的两层神经网络, 单样本向量化:                                                                                ...

  8. ES练习代码

    package elasticsearch; import java.util.HashMap; import java.util.List; import java.util.Map; import ...

  9. CentOS 7.5 安装与配置 Percona Server 5.7

    个人比较喜欢 MYSQL 的轻量,今天花了一点时间把阿里云上的 MYSQL5.7 换成了 Percona-Server .Percona 是一个开源的 MySQL 衍生版,TokuDB 的数据库引擎使 ...

  10. Android开发艺术探索学习笔记(十)

    第十章  Android的消息机制 面试中经常会被问到的一个问题:handler是如何在子线程和主线程中进行消息的传递的,这个问题通过了解Android的消息机制可以得到一个准确的答案. Androi ...