zoukankan      html  css  js  c++  java
  • 第一届山东省ACM——Balloons(java)

    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 and element B are both balloons. They are connected if:

    i) They are adjacent;

    ii) There is a list of element C1C2… 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<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.

    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

    HINT

     

    Source

    山东第一届省赛



    题目链接:http://acm.upc.edu.cn/problem.php?id=1929

    这道题不是很难,可能初次做的同学会觉得无从下手,思路很简单

       第一步:输入
         第二部:对输入的数据加墙,即把输入的数据用0括起来;假设输入

                      1 1 0 0 1
                      0 0 1 0 0
                      1 1 1 1 1
                      1 1 0 1 0
                      1 0 0 1 0

                 加墙:

                    0 0 0 0 0 0 0
                    0 1 1 0 0 1 0
                    0 0 0 1 0 0 0
                    0 1 1 1 1 1 0
                    0 1 1 0 1 0 0
                    0 1 0 0 1 0 0
                    0 0 0 0 0 0 0

        第三步:定义两个标志位数组a[n][n],b[n][n],分别对应第一种搜索方式,和第二种搜索方式

               第四步:对于第一种搜索方式:上下左右四个方向搜索,跳出条件是“无路可走”或者“标志位为1”(代表已经搜索过)——if(a[x][y]==0||b[x][y]==1){return ;}

            对于第二种搜索方式:八个方向搜索,就跳出条件与第一种一致

    代码:

    import java.util.Scanner;
    public class Main {
        public static int a[][];
        public static int b[][];
        public static int c[][];
        public static void dfs1(int x,int y){
            if(a[x][y]==0||b[x][y]==1){
                return ;
            }
            b[x][y]=1;
            dfs1(x,y+1);
            dfs1(x,y-1);
            dfs1(x-1,y);
            dfs1(x+1,y);
        
        }
        public static void dfs2(int x,int y){
            if(a[x][y]==0||c[x][y]==1){
                return ;
            }
            c[x][y]=1;
            dfs2(x,y+1);
            dfs2(x,y-1);
            dfs2(x-1,y);
            dfs2(x+1,y);
            dfs2(x+1,y+1);
            dfs2(x-1,y-1);
            dfs2(x-1,y+1);
            dfs2(x+1,y-1);
        }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int d=1;
            while(sc.hasNextInt()){
                
                int t=sc.nextInt();
                if(t==0)
                    break;
                a=new int[t+2][t+2];
                b=new int[t+2][t+2];
                c=new int[t+2][t+2];
                for(int i=1;i<t+1;i++){
                    String s=sc.next();
                    for(int j=1;j<t+1;j++){
                        a[i][j]=s.charAt(j-1)-'0';
                    }
                }
                for(int i=0;i<t+2;i++){
                    for(int j=0;j<t+2;j++){
                        System.out.print(a[i][j]+" ");
                    }
                    System.out.println();
                }
                int con1=0,con2=0;
                for(int i=1;i<t+1;i++){
                    for(int j=1;j<t+1;j++){
                        if(a[i][j]!=0&&b[i][j]==0){
                            con1++;
                            dfs1(i,j);
                        }
                    }
                }
                for(int i=1;i<t+1;i++){
                    for(int j=1;j<t+1;j++){
                        if(a[i][j]!=0&&c[i][j]==0){
                            con2++;
                            dfs2(i,j);
                        }
                    }
                }
                System.out.println("Case "+d+": "+con1+" "+con2);
                System.out.println();
                d++;
            }
        }
    }
    View Code

     

  • 相关阅读:
    grape入门
    半个小时写的一个二叉搜索树,实现了增,删,查功能
    Struts2 MVC 同 Spring MVC 的比较
    阿里巴巴 2016 java 实习岗位笔试题(昨天出炉)
    自己用20分钟java实现的单向链表(含有增删改查操作)
    关于 古人劝学 --写的真心是好 真的有收获
    JDK动态代理堆栈图详解--干货
    论闷声挣大钱与网红现象
    spring beanfactory --实现自己简单的 bean工厂类
    Spring IOC example one
  • 原文地址:https://www.cnblogs.com/asd1234/p/3637571.html
Copyright © 2011-2022 走看看