【HDU 6113 度度熊的01世界 】 & DFS
来源:互联网 发布:gal 知乎 编辑:程序博客网 时间:2024/06/04 22:33
度度熊的01世界 Accepts: 967 Submissions: 3064
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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,判断下多少快 0,1 即可
AC代码:
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int MAX = 1e2 + 10;typedef long long LL;char s[MAX][MAX];int n,m,vis[MAX][MAX];int fx[4] = {0,0,-1,1},fy[4] = {-1,1,0,0};void dfs(int x,int y,char c){ vis[x][y] = 1; for(int i = 0; i < 4; i++){ int xx = x + fx[i],yy = y + fy[i]; if(xx >= 0 && yy >= 0 && xx <= n && yy <= m && !vis[xx][yy] && s[xx][yy] == c){ dfs(xx,yy,c); } }}int main(){ while(~scanf("%d %d",&n,&m)){ for(int i = 1; i <= n; i++) scanf("%s",s[i] + 1); n++,m++; for(int i = 0; i <= n; i++) s[i][0] = s[i][m] = '0'; for(int i = 0; i <= m; i++) s[0][i] = s[n][i] = '0'; memset(vis,0,sizeof(vis)); int x1 = 0,x0 = 0; for(int i = 0; i <= n; i++) for(int j = 0; j <= m; j++){ if(!vis[i][j]){ if(s[i][j] == '1') x1++; else x0++; dfs(i,j,s[i][j]); } } if(x1 == 1 && x0 == 1) puts("1"); else if(x1 == 1 && x0 == 2) puts("0"); else puts("-1"); } return 0;}
- 【HDU 6113 度度熊的01世界 】 & DFS
- HDU 6113 度度熊的01世界 dfs
- 【dfs】hdu 6113 度度熊的01世界
- HDU 6113 度度熊的01世界 (DFS
- HDU 6113 度度熊的01世界(dfs)
- HDU 6113 度度熊的01世界 (暴力dfs)
- hdu 6113 度度熊的01世界(dfs)
- hdu 6113 度度熊的01世界 (dfs+扩边界)
- HDU 6113 度度熊的01世界(dfs)
- HDU 6113 度度熊的01世界(简单dfs)
- HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛
- 度度熊的01世界 dfs
- BestCoder 度度熊的01世界 (DFS)
- HDU6113 度度熊的01世界(dfs)
- hdu 6113 度度熊的01世界
- HDU-6113 度度熊的01世界
- HDU 6113 度度熊的01世界【搜索】
- hdu 6113 度度熊的01世界
- 编程挑战1——求逆置回文最少转换次数
- TPshop学习(9)银联支付
- leetcode Path Sum II
- [JS] jQuery与table
- 2018校招网易笔试——最长01交错子串
- 【HDU 6113 度度熊的01世界 】 & DFS
- POJ
- Visual Assist 返回下一个光标点的设置
- IDEA Spinrg boot 热部署配置
- 基于socket的TCP和UDP通讯的简单建立
- springioc容器学习第三篇----ApplicationContext容器初始化
- h5面试 &&和||的坑,你踩过吗?
- javascript中事件循环机制
- POJ2387 Til the Cows Come Home