Java Blue Bridge Cup Simulator Title (Part 2)

Spiral matrix

Problem description
  For a table with n rows and m columns, we can fill the table with positive integers in turn using the spiral method. We call the filled table a spiral matrix.
  For example, a spiral matrix with 4 rows and 5 columns is as follows:
  1 2 3 4 5
  14 15 16 17 6
  13 20 19 18 7
  12 11 10 9 8
Input format
  The first row of the input contains two integers n, m, respectively representing the spiral The number of rows and columns of the matrix.
  The second line contains two integers r, c, indicating the required line number and column number.
Output format
  outputs an integer representing the value of the element in row r and column c of the spiral matrix.
Sample input
4 5
2 2
Sample output
15
Evaluation use case size and agreement
  For 30% evaluation use cases, 2 <= n, m <= 20.
  For 70% of the evaluation use cases, 2 <= n, m <= 100.
  For all evaluation use cases, 2 <= n, m <= 1000, 1 <= r <= n, 1 <= c <= m.

import java.util.Scanner;

public class _007{
    static int [][] array = new int [1000][1000];  //定义数组及大小
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();  //行数
        int m = sc.nextInt();  //列数

        int r1 = sc.nextInt();
        int c1 = sc.nextInt();
        int num = 0;
        int r = 1,c = 0;
        while(num != n * m) {
            while(c < m && array[r][c+1] == 0)
                array[r][++c] = ++ num;
            while(r < n && array[r+1][c] == 0)
                array[++r][c] = ++ num;
            while(c > 1 && array[r][c-1] == 0)
                array[r][--c] = ++ num;
            while(r > 1 && array[r-1][c] == 0)
                array[--r][c] = ++ num;
        }
        System.out.println(array[r1][c1]);
    }
}

Wobble sequence

Problem description
  If the odd terms of a sequence are larger than the previous term and the even terms are smaller than the previous term, it is called a wobble sequence. That is a [2i] <a [2i-1], a [2i + 1]> a [2i].
  Xiaoming wants to know how many wobble sequences are m in length and each number is a positive integer between 1 and n.
Input format
  Input line contains two integers m, n.
Output format
  outputs an integer, indicating the answer. The answer may be large, please output the remainder divided by 10000.
Sample input
3 4
sample output
14
sample description The
  following is a swing sequence that meets the requirements:
  2 1 2
  2 1 3
  2 1 4
  3 1 2
  3 1 3
  3 1 4
  3 2 3
  3 2 4
  4 1 2
  4 1 3
  4 1 4
  4 2 3
  4 2 4
  4 3 4
Evaluation use case size and conventions
  For 20% evaluation use cases, 1 <= n, m <= 5;
  for 50% evaluation use cases, 1 <= n, m <= 10 ;
  For 80% of the evaluation use cases, 1 <= n, m <= 100;
  For all evaluation use cases, 1 <= n, m <= 1000.

public class _008 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] ch =new int[m+2][n+2];
        for (int i =1 ; i <= n ;i++){
            ch[1][i]=n-i+1;
        }
        for(int i=2;i<=m;i++){
            if((i&1)==1){
                for(int j=n;j>=1;j--)
                    ch[i][j]=(ch[i-1][j-1]+ch[i][j+1])%1000;
            }else{
                for(int j=1;j<=n;j++)
                    ch[i][j]=(ch[i-1][j+1]+ch[i][j-1])%1000;
            }
        }
        int result= (m&1)==1? ch[m][1]:ch[m][n];
        System.out.println(result);
    }
}

Household power

Description of the problem
  In 2015, households were able to access electricity throughout China. As a power builder, Xiaoming is helping countries along the Belt and Road to get electricity.
  This time, Xiaoming wants to help n villages get electricity. Among them, village 1 can just build a power station, and the electricity generated is enough for all villages.
  Now, there are no wires connected between the n villages. Xiaoming's main task is to erect wires to connect these villages so that all villages are directly or indirectly connected to the power station.
  Xiaoming measured the position (coordinates) and height of all villages. If you want to connect two villages, Xiaoming needs to spend the coordinate distance between the two villages plus the square of the height difference, which is formally described as the coordinates of (x_1, y_1) height The connection between the village with h_1 and the village with coordinates (x_2, y_2) and the height with h_2 is
  sqrt ((x_1-x_2) (x_1-x_2) + (y_1-y_2) (y_1-y_2)) + (h_1 -h_2) * (h_1-h_2).
  In the above formula, sqrt means taking the square root in parentheses. Please pay attention to the position of the brackets, the calculation method of the height is different from the calculation method of the horizontal and vertical coordinates.
  Due to limited funds, please help Xiaoming to calculate at least how much it will cost him to energize the n villages.
Input format
  The first line of input contains an integer n, which represents the number of villages.
  In the next n rows, each of the three integers x, y, and h represents the horizontal, vertical, and height of a village, respectively, and the first village can establish a power station.
Output format
  Outputs a line containing a real number, rounded to 2 decimal places to indicate the answer.
Sample input
4
1 1 3
9 9 7
8 8 6
4 5 4
Sample output
17.41
Evaluation use case size and convention
  For 30% evaluation use cases, 1 <= n <= 10;
  for 60% evaluation use cases, 1 <= n <= 100;
  for all evaluation use cases, 1 <= n <= 1000, 0 <= x, y, h <= 10000.

To be determined. . .

Xiaoming planting trees

Problem description
  Xiaoming went with his friends to plant trees in the suburbs. They brought some saplings carefully researched in their laboratory.
  There are n people in Xiao Ming and his friends. After careful selection, each person selected a suitable location for planting trees in a clearing, a total of n. They are going to plant all the saplings they brought.
  However, they encountered a difficulty: some saplings were relatively large, and some were too close together, causing the two trees to collide when planted.
  They saw the tree as a circle, with the center of the circle at the location they were looking for. If the circles corresponding to the two trees intersect, the two trees are not suitable for planting at the same time (tangent is not affected), which is called a conflict between the two trees.
  Xiaoming and his friends decided to sum up first and only plant a part of them to ensure that there are no conflicting trees. They also hope that these trees can cover the largest area (circle area).
Input format
  The first line of input contains an integer n, which represents the number of people, that is, the number of positions to be planted.
  The next n rows, each row of three integers x, y, r, represents the horizontal, vertical, and radius of a tree on the open ground.
Output format The
  output line contains an integer, which represents the sum of the area that can be planted without conflict. Since the area of ​​each tree is an integer multiple of pi, please output the value divided by the pi (it should be an integer).
Sample input
6
1 1 2
1 4 2
1 7 2
4 1 2
4 4 2
4 7 2
Sample output
12
Evaluation use case size and agreement
  For 30% evaluation use cases, 1 <= n <= 10;
  For 60% of the evaluation use cases, 1 <= n <= 20;
  for all evaluation use cases, 1 <= n <= 30, 0 <= x, y <= 1000, 1 <= r <= 1000.

import java.util.Scanner;

public class _010 {
    public static boolean[][] bool = new boolean[30][30];
    static boolean[] vis = new boolean[30];
    public static int[] x = new int[30];
    public static int[] y = new int[30];
    public static int[] r = new int[30];
    public static int n = 0, max = -1;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            x[i] = sc.nextInt();
            y[i] = sc.nextInt();
            r[i] = sc.nextInt();
        }
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                boolean bo = ((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) > (r[i] + r[j])
                        * (r[i] + r[j]));
                bool[i][j] = bo;
                bool[j][i] = bo;
            }
        }
        sc.close();
        dfs(1);
        System.out.println(max);
    }

    public static void dfs(int step) {
        if (step > n) {
            int sum = 0;
            for (int i = 1; i <= n; i++) {
                if (vis[i]) {
                    sum += (r[i] * r[i]);
                }
            }
            max = Math.max(sum, max);

            return;
        }

        vis[step] = false;
        dfs(step + 1);
        for (int i = 1; i < step; i++) {
            if (vis[i] && !bool[i][step]) {
                return;
            }

        }
        vis[step] = true;
        dfs(step + 1);
    }
}

Guess you like

Origin www.cnblogs.com/qimuz/p/12727916.html