Blue Bridge Cup: Xiao Ming used building blocks to build a castle (building blocks)

topic

[Problem description]
   Xiao Ming built a castle with building blocks.
   For convenience, Xiao Ming used a square volume of the same size when building it, and placed it on a grid graph with n rows and m columns, and each block occupies a small square of the grid graph.
   Of course, Xiao Ming's castle is not flat, but three-dimensional. Xiao Ming can place blocks on top of other blocks. When the blocks on a square are relatively high, it is a high tower, and when there are no blocks on a square, it is a flat ground.
   Xiao Ming’s castle can be represented by the number of blocks on each square. For example, the following represents a castle.
   9 3 3 1
   3 3 3 0
   0 0 0 0
   This castle has vacant land on the south and east sides, a large house on the northwest, a tall tower on the northwest corner, and a garage in the northeast corner.
   Now, Gege Witch is coming to destroy Xiao Ming's castle. He casts a spell to flood Xiao Ming's castle.
   If the height of the water is 1, the blocks that are close to the ground will be flooded. In the example above, 7 blocks will be flooded.
   If the water height is 2, more blocks will be flooded. In the example above, 13 blocks will be flooded.
   Given Xiao Ming's castle map, how many blocks will be flooded when the water height is 1, 2, 3, …, H.
[Input format]
   The first line of input contains two integers n, m.
   The next n rows, each with m integers, represent the number of layers of bricks in each position in Xiao Ming's castle.
   Next contains an integer H, which represents the upper limit of the water height.
[Output format]
   Output H lines, each line is an integer. The i-th integer represents the number of blocks that are flooded when the height of the water is i.
[Sample input]
3 4
9 3 3 1
3 3 3 0
0 0 0 0
10
[Sample output]
   7
   13
   19
   20
   21
   22
   23
   24
   25
   25
[Evaluation use case scale and conventions]
   For 40% of evaluation use cases, 1 <= n, m <= 100, 1 <= H <= 100, the number of building blocks does not exceed 100;
   for 70% of the evaluation cases, 1 <= n, m <= 1000, 1 <= H <= 1000, building blocks The number of layers does not exceed 1000;
   for all evaluation cases, 1 <= n, m <= 1000 , 1 <= H <= 100000, and the number of building blocks does not exceed 1000000000.

Code

import java.util.Scanner;

public class Main {
    
    //蓝桥杯要求class命名为Main,且无package
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int arr[][]=new int[n][m];
        for(int i=0;i<n;i++){
    
    
            for(int j=0;j<m;j++){
    
    
                arr[i][j]=scanner.nextInt();//赋值
            }
        }
        int h = scanner.nextInt();
        for(int high=1;high<=h;high++){
    
    
            int count=0;//被水淹没的积木,high的每次循环都会重新定义count
            for(int i=0;i<n;i++){
    
    
                for(int j=0;j<m;j++){
    
    
                    if(high>=arr[i][j]){
    
    
                        count+=arr[i][j];//如果水比房子高,被淹没的积木数为arr[i][j]上的所有积木
                    }
                    if(arr[i][j]>high){
    
    
                        count+=high;  //如果房子比水高,被淹没的积木数则为high
                    }
                }
            }
            System.out.println(count);
        }

    }
}


Problem analysis

   All in code

Guess you like

Origin blog.csdn.net/qq_47168235/article/details/108917934