当前位置: 首页 > 工具软件 > Balloons.IO > 使用案例 >

山东省第一届ACM程序设计大赛 Balloons (简单dfs)

陆弘新
2023-12-01

Balloons

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu
Submit

Status

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 and B is adjacent if

But to Kudo, element A and element B is adjacent if and

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

Source
山东省第一届ACM程序设计大赛2010.10.3


链接在这

题意:

其实就是把1连接起来,连接方式有两种
第一种,1可以和上下左右的1连接
第二种,1可以与周围8个方向的1连接
分别输出两种方式连接后的1,被分成了几部分


代码:

//Balloons (山东大一届省赛 简单dfs)
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int n;
char map1[105][105];
int vis1[105][105];
int vis2[105][105];
int dxy1[4][2]= {1,0,-1,0,0,1,0,-1};
int dxy2[8][2]= {1,0,1,1,1,-1,0,1,0,-1,-1,1,-1,0,-1,-1};

void dfs1(int x,int y)
{
    vis1[x][y]=1;
    for(int i=0; i<4; i++)
    {
        int nx=x+dxy1[i][0];
        int ny=y+dxy1[i][1];
        if(nx>=0&&nx<n&&ny>=0&&ny<n&&vis1[nx][ny]==0&&map1[nx][ny]=='1')
        {
            dfs1(nx,ny);
        }
    }
    return;
}

void dfs2(int x,int y)
{
    vis2[x][y]=1;
    for(int i=0; i<8; i++)
    {
        int nx=x+dxy2[i][0];
        int ny=y+dxy2[i][1];
        if(nx>=0&&nx<n&&ny>=0&&ny<n&&vis2[nx][ny]==0&&map1[nx][ny]=='1')
        {
            dfs2(nx,ny);
        }
    }
    return;
}

int main()
{
    int cont=0;
    while(scanf("%d",&n)&&(n!=0))
    {
        cont++;
        memset(vis1,0,sizeof(vis1));
        memset(vis2,0,sizeof(vis2));
        for(int i=0; i<n; i++)
                scanf("%s",map1[i]);
        int ans1=0,ans2=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(vis1[i][j]==0&&map1[i][j]=='1')
                {
                    ans1++;
                    dfs1(i,j);
                }
                if(vis2[i][j]==0&&map1[i][j]=='1')
                {
                    ans2++;
                    dfs2(i,j);
                }
            }
        }
        printf("Case %d: %d %d\n\n",cont,ans1,ans2);
    }
    return 0;
}
 类似资料: