HUAWEI Autumn Recruitment Written Exam Questions

1. Epidemic prevention in pig farms

Lao Li contracted a pig farm many years ago and introduced several breeding pigs. After these years of operation, there are now N pigs in the pig farm, numbered from 0 to N-1 (every pig has a unique number regardless of life or death). number);
Lao Li recorded the sows and piglets produced when each pig was born, in the format: x y1 y2 y3...([Note: x is the mother pig, y1, y2, y3... are For newborn pigs, the above codes are all within 0...N-1, each pig can be born multiple times, and each pig has only one mother pig); for the needs of epidemic prevention, it is necessary to check whether any two pigs are related (
two Pigs have the same ancestors), and calculate the closeness of the relationship (relationship distance, the distance of the same number is 0).

解答要求
时间限制:CIC++1000ms.其他语言:2000ms内存限制:CIC++ 256MB,其他语言:512MB
输入
第一行输入总数N
第二行表示后续生产记录行数M,
后续M行输入生产记录,以空格分隔x y1 y2 y3
最后一行输入m1, m2:表示待检查的m1和m2编号【取值范围】
0<N<=1000000000 0<=M<10000
输出
一个整数,表示m1和m2之间的关系距离,无亲戚关系输出-1
样例1
输入:
3
1
0 1 2
0 1
输出:1
解释:0号生产了1号和2号
所以0号和1号是有亲戚关系(0-1),且关系距离为1

2. Quick fix

In an MxN block, there is a soldier S and an enemy E, mark X as a block that cannot pass, and mark B as a block that can pass; soldiers can move from one block to adjacent blocks in one unit time (soldiers You can only move one block horizontally or vertically at a time); each time a soldier changes direction, it takes an additional unit of time (when a soldier moves a block for the first time, it does not need to consider its initial direction, that is, it only needs to — unit time to reach the adjacent block). Calculate the minimum time for soldier S to reach the block where E is located.

解答要求
时间限制:C/C++1000ms,其他语言:2000ms
内存限制:CIC++ 256MB,其他语言:512MB
输入
第一行为两个数字,表示街区的大小,M行,N列;(1年M,N <=1000,M、N不同时为1)
接下来M行,每行N个字母,字母S表示士兵所在街区,字母E表示敌人所在街区,字母X表示障碍,字母B表示可以经过的街区。(只有1个S,一个E)
输出
最少需要的时间,当士兵S永远无法到达敌人E所在的街区时,输出-1
样例1
输入:
6 6
SBBBBB
BXXXXB
BBXBBB
XBBXXB
BXBBXB
BBXBEB
输出:
13

3. Smart parking

A company parking lot has installed a smart parking system. When a vehicle enters the parking lot, the system will automatically calculate a shortest path and light up the guide lights on the ground for this path. If there is no available parking space, it will light up at the entrance Red light, as shown below is the floor plan of a parking lot (up north, down south, left west, right east), and the opening is the entrance of the parking lot.
In order to simplify the processing, we use the starting lane coordinates to indicate the entrance of the parking lot, and use a two-dimensional array to record the current parking situation, such as:

8 15 8 1
1 2 2 1 2 2 1 2 2  1 2 2 1 2 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 2 1 0 1 3 2 2 2 1 2 2 2 
1 2 1 2 2 0 1 3 1 2 1 2 1 2 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 1 2 2 2 0 2 2 1 1 0 1 2 1 2
2 2 2 2 2 2 0 2 2 2 0 2 2 2 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

1.8 15 means the number of rows M of the matrix is ​​8, and the number of columns N is 15;
2.81 means the coordinates of the entrance (rows and columns respectively, and the numbers of rows and columns start counting from 1): 8 means the eighth row, 1 means the first column, Separated by spaces; it means that the green light starts to light up from this coordinate point at the entrance;
3. The matrix data represents the parking lot situation: 0 represents the lane; 1 represents the empty parking space; 2 represents the parking space; 3 represents the pillar, which cannot pass
; Depending on the parking lot situation, light up a shortest path to an available parking space.
Routing rules:
1. All parking spaces are north-south, and vehicles can only park in the parking spaces from the south or north lanes of the parking spaces, and cannot park laterally;
2. If there are target parking spaces at the same distance, the one with the smaller row coordinates is preferred, Secondly, choose the one with the smaller column coordinates: In the figure: 610 and 612 choose 610; 17 and 37 choose 17; there are multiple paths with the same distance to the same parking space, and the path node row coordinates and the smaller one are preferred. , then select the column coordinates and the smaller one
without checking the validity of the input;

解答要求
时间限制:C/C++1000ms,其他语言:2000ms内存限制:C/C++256MB,其他语言:512MB
给定一个起点坐标:i j和一个M*N的停车场二维数组描述图;M(5<=M<= 100) N(5<=N <=100)i (1 <=i<= M)j(1<=j <=N)
样例1
输入:
5 6 2 1
2 1 2 2 1 2 
0 0 0 0 0 0
2 1 2 1 2 1 
1 1 2 1 2 2 
0 0 0 0 0 0
输出:
2 1 2 2 1 2
解释:
1.入口为21,且11和31车位已经占用,向前到2 2;
2.1 2和3 2都可以停车,取行坐标较小的1 2进行停车;
3.输出路径为2 1 2 2 1 2
样例2
输入:
5 9 5 5
2 2 1 2 1 2 1 2 1
0 0 0 0 0 0 0 0 0
2 2 1 1 0 1 2 1 2 
2 2 2 2 0 2 2 2 2
0 0 0 0 0 0 0 0 0
输出:
5 5 4 5 3 5 2 5 1 5
解释:
1.入口为5 5;
2.3 4和3 6是3 5的横向相邻坐标,不能直接停车;
3.1 5的停车步数最小,直接进行停车;

Guess you like

Origin blog.csdn.net/mathlxj/article/details/131790505