• sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)


    Balloons

    Time Limit: 1000MS Memory limit: 65536K

    题目描述

    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?

    输入

    The input consists of several test cases.
    The first line of input in each test case contains one integer N (0<N100), which represents the size of the matrix.
    Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string only consists of 0 and 1, while 0 represents a block and 1represents balloons.
    The last case is followed by a line containing one zero.

    输出

     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.

    示例输入

    5
    11001
    00100
    11111
    11010
    10010
    
    0

    示例输出

    Case 1: 3 2

    提示

     

    来源

     2010年山东省第一届ACM大学生程序设计竞赛
     

      DFS搜索,求连通分量的个数。需要注意的是第一个只能是4个方向,而第二个可以有8个方向,即可以斜着走。

      代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 using namespace std;
     4 int n;
     5 int dx[4] = {0,-1,0,1};
     6 int dy[4] = {-1,0,1,0};
     7 int Dx[8] = {0,-1,-1,-1,0,1,1,1};
     8 int Dy[8] = {-1,-1,0,1,1,1,0,-1};
     9 
    10 bool judge(char a[101][101],int x,int y)
    11 {
    12     if(x<0 || y<0 || x>=n || y>=n)
    13         return 1;
    14     if(a[x][y]!='1')
    15         return 1;
    16     return 0;
    17 }
    18 void dfs1(char a[][101],int x,int y)
    19 {
    20     a[x][y] = '0';
    21     for(int i=0;i<4;i++){
    22         int cx = x+dx[i];
    23         int cy = y+dy[i];
    24         if(judge(a,cx,cy))
    25             continue;
    26         dfs1(a,cx,cy);
    27     }
    28 }
    29 void dfs2(char a[][101],int x,int y)
    30 {
    31     a[x][y] = '0';
    32     for(int i=0;i<8;i++){
    33         int cx = x+Dx[i];
    34         int cy = y+Dy[i];
    35         if(judge(a,cx,cy))
    36             continue;
    37         dfs2(a,cx,cy);
    38     }
    39 }
    40 int main()
    41 {
    42     int Count=1;
    43     while(cin>>n){
    44         if(n==0) break;
    45         int num1 = 0,num2 = 0;
    46         char a[101][101];
    47         char b[101][101];
    48         for(int i=0;i<n;i++){
    49             cin>>a[i];
    50         }
    51         for(int i=0;i<n;i++)
    52             for(int j=0;j<n;j++)
    53                 b[i][j]=a[i][j];
    54         int i,j;
    55         for(i=0;i<n;i++)
    56             for(j=0;j<n;j++){
    57                 if(a[i][j]=='1'){
    58                     num1++;
    59                     dfs1(a,i,j);
    60                 }
    61             }
    62         for(i=0;i<n;i++)
    63             for(j=0;j<n;j++){
    64                 if(b[i][j]=='1'){
    65                     num2++;
    66                     dfs2(b,i,j);
    67                 }
    68             }
    69         cout<<"Case "<<Count++<<": "<<num1<<' '<<num2<<endl<<endl;
    70     }
    71     return 0;
    72 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    范式理论
    事务
    触发器练一练
    Javascript模块化编程(三):require.js的用法
    Javascript模块化编程(二):AMD规范
    Javascript模块化编程(一):模块的写法
    什么是比特币?这可能是最通俗易懂的答案了
    Apache服务器的Options 的 Indexes FollowSymLinks详解
    机器学习进阶-图像金字塔与轮廓检测-图像金字塔(拉普拉斯金字塔)
    机器学习进阶-图像金字塔与轮廓检测-图像金字塔-(**高斯金字塔) 1.cv2.pyrDown(对图片做向下采样) 2.cv2.pyrUp(对图片做向上采样)
  • 原文地址:https://www.cnblogs.com/yym2013/p/3662190.html
Copyright © 2020-2023  润新知