欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字节跳动2019届校招笔试题

程序员文章站 2022-07-13 14:17:37
...

1.世界杯开幕式会在球场C举行,球场C的球迷看台可以容纳M*N个球迷。在球场售票完成后,现官方想统计此次开幕式一共有多少个球队球迷群体,最大的球队球迷群体有多少人。

经调研发现,球迷群体在选座时有以下特性:

同球队的球迷群体会选择相邻座位,不同球队的球迷群体会选择不相邻的座位(注解:相邻包括前后相邻,左右相邻,斜对角相邻)

给定一个M*N的二维球场,0代表该位置没有坐人,1代表该位置已有选择,希望输出球队群体个数P,最大的球队群体人数Q

输入描述:

第一行,2个数字,M及N,使用英文逗号分隔

接下来M行,每行N的数字,使用英文逗号分隔

输出描述:

一行,2个数字,P及Q,使用英文逗号分隔

其中P表示球队群体个数,Q表示最大的球队群体人数

例:输入

10,10

0,0,0,0,0,0,0,0,0,0

0,0,0,1,1,0,1,0,0,0

0,1,0,0,0,0,0,1,0,1

1,0,0,0,0,0,0,0,1,1

0,0,0,1,1,1,0,0,0,1

0,0,0,0,0,0,1,0,1,1

0,1,1,0,0,0,0,0,0,0

0,0,0,1,0,1,0,0,0,0

0,0,1,0,0,1,0,0,0,0

0,1,0,0,0,0,0,0,0,0

输出:6,8

java实现:

public class Problem1 {
    static String input = "10,10\n" +
            "0,0,0,0,0,0,0,0,0,0\n" +
            "0,0,0,1,1,0,1,0,0,0\n" +
            "0,1,0,0,0,0,0,1,0,1\n" +
            "1,0,0,0,0,0,0,0,1,1\n" +
            "0,0,0,1,1,1,0,0,0,1\n" +
            "0,0,0,0,0,0,1,0,1,1\n" +
            "0,1,1,0,0,0,0,0,0,0\n" +
            "0,0,0,1,0,1,0,0,0,0\n" +
            "0,0,1,0,0,1,0,0,0,0\n" +
            "0,1,0,0,0,0,0,0,0,0";

    static class Person {
        int num;
        int reach;

        public Person(int num, int reach) {
            this.num = num;
            this.reach = reach;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "num=" + num +
                    ", reach=" + reach +
                    '}';
        }
    }

    static Person[][] handleInput() {
        String[] lines = input.split("\n");
        String[] first = lines[0].split(",");
        int row = Integer.valueOf(first[0]);
        int col = Integer.valueOf(first[1]);
        Person[][] arr = new Person[row][col];
        String[] line;
        for (int i = 1; i < lines.length; i++) {
            line = lines[i].split(",");
            for (int j = 0; j < line.length; j++) {
                arr[i - 1][j] = new Person(Integer.valueOf(line[j]), 0);
            }
        }
        return arr;
    }

    static void searchMaxPerson(Person[][] arr) {
        int row = arr.length;
        int col = arr[0].length;
        int tmp ;
        int max = 0;
        int count = 0;
        Person cur;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                cur = arr[i][j];
                if (cur.num != 0 && cur.reach == 0) {
                    count ++;
                    tmp = searchAll(arr,i,j);
                    if(tmp > max){
                        max = tmp;
                    }
                }
            }
        }
        System.out.println(count+","+max);
    }

    static int searchAll(Person[][] arr, int r, int c) {
        int row = arr.length;
        int col = arr[0].length;
        if (r >= row || r < 0 || c >= col || c < 0) {
            return 0;
        }
        Person cur = arr[r][c];
        if (cur.num != 0 && cur.reach == 0) {
            cur.reach = 1;
            int n1 = searchAll(arr, r - 1, c) + searchAll(arr, r + 1, c);
            int n2 = searchAll(arr, r - 1, c - 1) + searchAll(arr, r + 1, c + 1);
            int n3 = searchAll(arr, r, c - 1) + searchAll(arr, r, c + 1);
            int n4 = searchAll(arr, r - 1, c + 1) + searchAll(arr, r + 1, c - 1);
            return n1 + n2 + n3 + n4 + 1;
        }
        return 0;
    }

    public static void main(String[] args) {
        Person[][] arr = handleInput();
        searchMaxPerson(arr);
    }
}
相关标签: 字节跳动笔试