2152 Balloons(两遍bfs求图的连通块)

Problem Description

Both Saya and Kudo like balloons. One day, they heard that in the central park, there will be thousands of people fly balloons to pattern a big image.
They were very interested about this event, and also curious about the image.
Since there are too many balloons, it is very hard for them to compute anything they need. Can you help them?
You can assume that the image is an N*N matrix, while each element can be either balloons or blank.
Suppose element A and element B are both balloons. They are connected if:
i) They are adjacent;
ii) There is a list of element C1, C2, … , Cn, while A and C1 are connected, C1 and C2 are connected …Cn and B are connected.
And a connected block means that every pair of elements in the block is connected, while any element in the block is not connected with any element out of the block.
To Saya, element A(xa,ya)and B(xb,yb) is adjacent if |xa-xb| + |ya-yb| ≤ 1
But to Kudo, element A(xa,ya) and element B (xb,yb) is adjacent if |xa-xb|≤1 and |ya-yb|≤1
They want to know that there’s how many connected blocks with there own definition of adjacent?

Input

The input consists of several test cases.
The first line of input in each test case contains one integer N (0

Output

For each case, print the case number (1, 2 …) and the connected block’s numbers with Saya and Kudo’s definition. Your output format should imitate the sample output. Print a blank line after each test case.

Sample Input

5
11001
00100
11111
11010
10010
0

Sample Output

Case 1: 3 2

解题思路:两遍bfs分别求连通块的个数

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<memory.h>
#include<queue>
using namespace std;
typedef struct node {int x;int y;
} Node;
Node a[10005];//记录1出现的位置,降低时间复杂度,以空间换时间
Node temp,ts;
char m[105][105];//保存图
int book[105][105];//标记数组
queue<Node>q;
int next1[4][2]={{-1,0},{1,0},{0,1},{0,-1}};//控制方向
int next2[8][2]={{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,1},{1,-1}};
int main() {int n,z,k;int tt=0;while(cin>>n&&n) {tt++;getchar();memset(a,0,sizeof(a));memset(book,0,sizeof(book));memset(m,0,sizeof(m));k=0;for(int i=1; i<=n; i++) {for(int j=1; j<=n; j++) {cin>>m[i][j];if(m[i][j]!='0') {a[k].x=i;a[k].y=j;k++;}}getchar();//注意吸收换行}int sum1=0;for(int i=0; i<k; i++) {//对每一个1的位置遍历if(book[a[i].x][a[i].y]==0&&m[a[i].x][a[i].y]=='1') {sum1++;//如果这个点没走过,并且可以走则形成一个连通块q.push(a[i]);book[a[i].x][a[i].y]=1;while(!q.empty()) {//第一遍bfs求上下左右四个方向上的连通块temp=q.front();q.pop();int xx=temp.x;int yy=temp.y;for(int j=0; j<4; j++) {int txx=xx+next1[j][0];int tyy=yy+next1[j][1];if(book[txx][tyy]==0&&m[txx][tyy]=='1') {book[txx][tyy]=1;temp.x=txx;temp.y=tyy;q.push(temp);}}}}}int sum2=0;memset(book,0,sizeof(book));//注意要初始化for(int i=0; i<k; i++) {if(book[a[i].x][a[i].y]==0&&m[a[i].x][a[i].y]=='1') {sum2++;q.push(a[i]);book[a[i].x][a[i].y]=1;while(!q.empty()) {//第二遍bfs求八个方向上的连通块temp=q.front();q.pop();int xx=temp.x;int yy=temp.y;for(int i=0; i<8; i++) {int txx=xx+next2[i][0];int tyy=yy+next2[i][1];if(book[txx][tyy]==0&&m[txx][tyy]=='1') {book[txx][tyy]=1;temp.x=txx;temp.y=tyy;q.push(temp);}}}}}cout<<"Case "<<tt<<": "<<sum1<<" "<<sum2<<endl;cout<<endl;}return 0;
}/***************************************************
User name: dlili
Result: Accepted
Take time: 4ms
Take Memory: 328KB
Submit time: 2018-04-10 20:47:20
****************************************************/

2152 Balloons(两遍bfs求图的连通块)相关推荐

  1. 白魔法师--图的连通块问题(牛客小白月赛25)

    链接:题目链接 来源:牛客网 白魔法师 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 你 ...

  2. 图中连通块的个数:并查集

    图的连通性问题 在地图上有若干城镇(点),已知所有有道路直接相连的城镇对.要解决整幅图的连通性问题.比如,随意给你两个点,让你判断它们是否连通:或者问你整幅图一共有几个连通块,也就是被分成了几个互相独 ...

  3. *【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)

    题干: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...

  4. CSP认证201703-4 地铁修建[C++题解]:连通路径上的最大边权最小、bfs求边权为1的最短路、二分查找

    文章目录 题目解答 题目链接 题目解答 来源:acwing 分析: 题目给定n个点和m条边,要求最多选择n条边,使得1到n连通,然后每段路同时开工,求最小工时.换句话说,求的是连通路上最大边权最小. ...

  5. BZOJ 4242 水壶(BFS建图+最小生成树+树上倍增)

    题意 JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长方形,每个区域都是建筑物.原野.墙壁之一.建筑物的区域有P个,编号为1...P. JOI君只能进入建筑 ...

  6. Magic Potion(最大流,跑两遍网络流或者加一个中转点)

    Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...

  7. 洛谷 P1629 - 邮递员送信(往返两遍dijkstra)

    目录 题目描述 C++代码 题目描述 有一个邮递员要送东西,邮局在节点 1 1 1.他总共要送 n − 1 n-1 n−1 样东西,其目的地分别是节点 2 2 2 到节点 n n n.由于这个城市的交 ...

  8. HDU5886 Tower Defence 【两遍树形dp】【最长链预处理】

    题意:N个点的一棵带权树.切掉某条边的价值为切后两树直径中的最大值.求各个边切掉后的价值和(共N-1项). 解法一: 强行两遍dp,思路繁琐,维护东西较多: dis表示以i为根的子树的直径,dis2表 ...

  9. 图的深度搜索c语言,求图的深度优先搜索!该怎么处理

    当前位置:我的异常网» C语言 » 求图的深度优先搜索!该怎么处理 求图的深度优先搜索!该怎么处理 www.myexceptions.net  网友分享于:2013-03-16  浏览:12次 求图的 ...

最新文章

  1. 打算看的书或正在看的书
  2. MySQL Online DDL的改进与应用
  3. android java.lang.IllegalArgumentException: Comparison method violates its general contract! 问题
  4. 办公室影响同事关系的九种行为
  5. Linux技巧:自动挂载UDF光盘的技巧
  6. nc——制作后门连接与反弹shell连接的使用
  7. python王者归来 pdf_OpenStack开源云:王者归来 PDF 下载
  8. zookpeer实现对服务器动态上下线的监听
  9. python查看内存地址的内容_python中如何查看指定内存地址的内容
  10. 玛塔留言板无刷新留言板程序
  11. Trade off between bias and variance
  12. 第三章-电商项目-优化评论分页查询
  13. 详解Intellij IDEA中.properties文件中文显示乱码问题的解决
  14. 诺基亚7P可刷华为鸿蒙系统,今日热闻 | 鸿蒙OS 2.0发布、EMUI 11发布、iPhone 12或配备7P镜头...
  15. App Cleaner Uninstaller卸载清理工具 for mac
  16. 《超实用的HTML代码段》阅读笔记1——HTML5自动聚焦
  17. 移动端overflow-x去掉滑动条
  18. 程序员怒怼产品经理最新表情包,叫我改Bug这辈子是不可能的
  19. Python学习教程:数据类型—字符串大总结
  20. PhotoZoom Pro 7 支持哪些图像格式?

热门文章

  1. GDB调试工具基本命令总结
  2. 宁波大学计算机非全,宁波大学非全日制研究生管理办法(试行)
  3. iOS(Swift) 二维码扫描
  4. python爬虫抓取为空,或者网页提示:很抱歉,我们目前暂时不支持 IE 浏览器...
  5. 一个小小的赛马文字图片游戏 很多不足点 比如只知道第一名 不知道后三名 不过基本思想有了...
  6. 建工计算机在线使用,建工计算器创建公式的相关操作教程
  7. 解决axios请求超时
  8. Tensorflow2.0学习笔记(二)北大曹健老师教学视频第五讲
  9. Linux下基于c++的简单五子棋小游戏
  10. javafx一个stage窗口弹出,主窗口跟随弹出(聚焦二级窗口时,在二级窗口后面显示主窗口)