Fellow me on GitHub

HDU6113

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

 
 1 //2017-08-12
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <queue>
 7 
 8 using namespace std;
 9 
10 const int N = 210;
11 char G[N][N];
12 int n, m;
13 int dx[4] = {0, 1, 0, -1};
14 int dy[4] = {1, 0, -1, 0};
15 bool vis[N][N];
16 struct Node{
17     int x, y;
18     Node(int _x, int _y):x(_x), y(_y){}
19 };
20 
21 void bfs(int x, int y, int op){
22     Node node(x, y);
23     memset(vis, 0, sizeof(vis));
24     vis[x][y] = op+1;
25     G[x][y] = '#';
26     queue<Node> q;
27     q.push(node);
28     while(!q.empty()){
29         Node a = q.front();
30         q.pop();
31         for(int i = 0; i < 4; i++){
32             int nx = a.x + dx[i];
33             int ny = a.y + dy[i];
34             if(nx>=0 && nx < n && ny>=0 && ny < m && !vis[nx][ny] && G[nx][ny] == '0'+op){
35                 Node tmp(nx, ny);
36                 q.push(tmp);
37                 vis[nx][ny] = op+1;
38                 G[nx][ny] = '#';
39             }
40         }
41     }
42 }
43 
44 int main()
45 {
46     //freopen("data1006.txt", "r", stdin);
47     while(scanf("%d%d", &n, &m)!=EOF){
48         for(int i = 0; i < n; i++){
49               scanf("%s", G[i]);
50         }
51         for(int i = 0; i < m; i++){
52             if(G[0][i] == '0')bfs(0, i, 0);
53             if(G[n-1][i] == '0')bfs(n-1, i, 0);
54         }
55         for(int i = 0; i < n; i++){
56             if(G[i][0] == '0')bfs(i, 0, 0);
57             if(G[i][m-1] == '0')bfs(i, m-1, 0);
58         }
59         int cnt1 = 0;
60         for(int i = 0; i < n; i++){
61             for(int j = 0; j < m; j++){
62                 if(G[i][j] == '1'){
63                     bfs(i, j, 1);
64                     cnt1++;
65                 }
66             }
67         }
68         int cnt2 = 0;
69         for(int i = 0; i < n; i++){
70             for(int j = 0; j < m; j++){
71                 if(G[i][j] == '0'){
72                     bfs(i, j, 0);
73                     cnt2++;
74                 }
75             }
76         }
77         if(cnt1==1 && cnt2 == 0)printf("1\n");
78         else if(cnt1 == 1 && cnt2 == 1)printf("0\n");
79         else printf("-1\n");
80     }
81 
82     return 0;
83 }

 

 

posted @ 2017-08-12 21:56  Penn000  阅读(219)  评论(0编辑  收藏  举报