Huawei OD Computer Test Real Questions-Numerical Assimilation-2023 OD Unified Examination (Paper B)

Topic description:

There is an m*n two-dimensional array whose members range from 0,1,2. The elements with a value of 1 have assimilation properties. Every time 1S passes, the elements with a value of 0 for the top, bottom, left, and right are assimilated to 1. Elements with a value of 2 are immune to assimilation. Randomly initialize all members of the array to 0 or 2, then modify the [0,0] elements of the matrix to 1. After a long enough time, find how many elements in the matrix are 0 or 2 (that is, the number of 0 and 2 Sum).

Enter description:

The first two numbers entered are the matrix size. The following numbers are the matrix contents.

Output description:

Returns the number of non-1 elements in the matrix

Additional instructions:

m and n will not exceed 30 (inclusive).

Example 1

enter:

4 4
0 0 0 0
0 2 2 2
0 2 0 0
0 2 0 0

Output:

9

Description: The first two numbers of the input number are the matrix size. The following numbers are matrix contents.
The contents of this matrix are as follows:

  0,0,0,0
  0,2,2,2
  0,2,0,0
  0,2,0,0
}

After the starting position (0,0) is modified to 1, the final assimilation matrix can only be:


  1,1,1,1
  1,2,2,2
  1,2,0,0
  1,2,0,0
}

Therefore, the number of non-1 elements in the matrix is ​​9.

m, n = map(int, input().split())
matrix = []
for _ in range(m):
    matrix.append(list(map(i

Guess you like

Origin blog.csdn.net/2301_76848549/article/details/133135012