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

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
****************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值