[Huawei Computer Test Real Questions Java] Parent-child Game

  Table of contents

Question description

Enter description

Output description

Reference example

Reference Code

Computer test introduction

write at the end


Question description

The baby and mother participate in a parent-child game. On a two-dimensional matrix (N*N) grid map, the baby and the mother draw lots to determine their respective positions. Each grid on the map has a different number of candies, and some grids have obstacles.

The rules of the game are that the mother must reach the baby's position in the shortest time (only one step per unit time). All candies on the road can be taken away. She cannot walk on the obstacle grid and can only walk up, down, left, and right.

Please ask the mother how many candies she can get at most in the shortest time to reach the baby's location (priority is given to getting as many candies as possible while arriving in the shortest time).

Enter description

The first line of input is N, N represents the size of the two-dimensional matrix

Then there are N rows, each row has N values, the value of each position of the table matrix, where:

  • -3: Mom
  • -2: baby
  • -1: Obstacle
  • ≥0: number of candies (0 means no candies, but you can go)

Output description

Output the maximum number of candies the mother can get in the shortest time to reach the baby's position, with no extra spaces at the end of the line.

Remark

The maximum map size is 50*50

Reference example

Example 1

enter

4
3 2 1 -3
1 -1 1 1
1 1 -1 2
-2 1 2 3

output

9

Example 2

enter

4
3 2 1 -3
-1 -1 1 1
1 1 -1 2
-2 1 -1 3

output

-1

Reference Code

import java.util.LinkedList;
import java.util.Scanner;
 
public class Main {
  static int n;
  static int[][] matrix;
  static int[][] candy;
  static int[][] offsets = {
   
   {1, 0}, {0, -1}, {-1, 0}, {0, 1}};
 
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
 
    n = sc.nextInt();
 
    matrix = new int[n][n];
    candy = new int[n][n];
 
    LinkedList<Integer> queue = new LinkedList<>();
 
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        candy[i][j] = -1;
 
        matrix[i][j] = sc.nextInt();
        // 妈妈的位置
        if (matrix[i][j] == -3) {
          candy[i][j] = 0;
          queue.add(i * n + j); // 二维坐标一维化
        }
      }
    }
 
    // 记录题解
    int ans = -1;
 
    // bfs 按层扩散
    while (queue.size() > 0) {
      // 记录当前扩散层的点
      LinkedList<Integer> newQueue = new LinkedList<>();
 
      // 当前层是否有宝宝所在的点
      boolean flag = false;
 
      for (int pos : queue) {
        // 源点坐标
        int x = pos / n;
        int y = pos % n;
 
        // 向四个方向扩散
        for (int[] offset : offsets) {
          // 当前扩散点坐标
          int newX = x + offset[0];
          int newY = y + offset[1];
 
          // 当前扩散点坐标越界,或者扩散点是墙,则无法扩散
          if (newX < 0 || newX >= n || newY < 0 || newY >= n || matrix[newX][newY] == -1) continue;
 
          // 当前扩散点坐标对应的糖果数量为-1,说明对应扩散点坐标位置还没有加入到当前扩散层
          if (candy[newX][newY] == -1) {
            newQueue.addLast(newX * n + newY); // 加入当前扩散层
          }
 
          // 当前扩散点可能会被多个源点扩散到,因此比较保留扩散过程中带来的较大糖果数
          // candy[newX][newY] 记录的是当前扩散点获得的糖果数
          // candy[x][y] + Math.max(0, matrix[newX][newY]) 记录的是从源点(x,y)带来的糖果数 + (newX,newY)位置原本的糖果数
          candy[newX][newY] =
              Math.max(candy[newX][newY], candy[x][y] + Math.max(0, matrix[newX][newY]));
 
          // 如果当前扩散点是宝宝位置,则可以停止后续层级的bfs扩散,因为已经找到宝宝的最短路径长度(即扩散层数)
          if (matrix[newX][newY] == -2) {
            ans = candy[newX][newY];
            flag = true;
          }
        }
      }
 
      // 已经找到去宝宝位置的最短路径和最大糖果数,则终止bfs
      if (flag) break;
 
      // 否则继续
      queue = newQueue;
    }
 
    System.out.println(ans);
  }
}

Computer test introduction

Three questions of medium difficulty, with a total score of 400 points. The first two questions are both 100 points, and the last one is 200 points. The time limit for each question is 1S (conventional algorithms cannot solve it, and violent methods can be used). The
computer-based test scores are based on The score is calculated based on the proportion of the number of passed test cases (if you cannot solve the last question, you can write out the results of the boundary conditions, etc.)
. The computer-based test lasts 2 and a half hours. If the goal is to sprint for the highest score in the computer-based test, the time for completing the questions will be allocated. The first two questions take 40 minutes, and the last question takes 70 minutes (you can follow the 40-minute time limit for normal practice). The
score line for the computer-based test is different depending on the recruitment target colleges and non-target colleges (different Huawei recruitment departments will have differences)

write at the end


Creation is not easy!

If you think the content is helpful to you, please give me a follow and support me!

If there are any errors, please point them out in the comment area and I will change them in time!


Thank you for reading. The article is mixed with personal understanding. If there are any errors, please contact me to point them out ~

Guess you like

Origin blog.csdn.net/forest_long/article/details/135307223