zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    概率论——随机事件及其概率
    Web应用程序项目以配置使用IIS。未找到Web服务器”链接地址”
    LaTeX中.sty文件缺失解决办法
    IIS中的经典模式和集成模式有什么区别
    判断有序整型数组中是否存在两数,相加之和等于给定的任意整数
    51Job的搜索技巧
    登录失败。该登录名来自不受信任的域,不能与 Windows 身份验证一起使用。
    Ubuntu使用Latex模板moderncv写简历
    COM相关操作(C#)
    什么是单线程单元(STA)什么是多线程单元(MTA)
  • 原文地址:https://www.cnblogs.com/yym2013/p/3662190.html
Copyright © 2011-2022 走看看