DatabricksOA #25
Write 'L' on Matrix
简单可在线判题
考点(AI 标注)模拟/实现哈希计数
You are given an n×n matrix of odd order whose elements can only be 0, 1, or 2. One operation changes a cell to a different value. Let m=n//2 and use 0-based coordinates; the specified shape is the union of the following coordinates: (t,t) and (t,n−1−t) for 0≤t≤m, and (t,m) for m≤t<n. That is, the two upper half-diagonals meet at the center, and a vertical line then extends from the center down to the bottom edge. All cells inside the shape must become one value a, and all cells outside the shape must become a different value b, with a,b∈{0,1,2} and a≠b. Find the minimum number of cells to change. The center counts as a single cell. This problem follows its explicit coordinate definition rather than guessing the pattern from the letter L or X.
输入格式
The first line contains an odd n, followed by n lines of n integers each. n takes the values 3,5,7,…,99, and matrix values are 0..2. The source of the range is explicit: the fastprep version of the same problem gives odd 5..99, and the original examples of both sources also include n=3, so this site uses the union of the two; the OCR itself has no numeric upper bound, and this site does not add the unsupported n=1.
输出格式
Output the minimum number of cells to change, as an integer. The shape and the background must use different values.
样例 1
输入:
3
1 0 2
1 2 0
0 2 0
输出:
2
样例 2
输入:
5
2 0 0 0 2
1 2 1 2 0
0 1 2 1 0
0 0 2 1 1
1 1 2 1 1
输出:
8
样例 3
输入:
3
0 0 0
0 0 0
0 0 0
输出:
4
样例说明
The first two examples come from the original OCR, with their answers kept as 2 and 8. Example 1 changes (0,0) to 2 and (1,0) to 0. Example 2 changes the 8 background 0s to 1; the fastprep input omits row 4 [0,0,2,1,1], and this site restores the complete original 5×5 matrix from OCR021/022. The third example, an all-0 3×3 matrix, is added by this site: changing the 4 shape cells to another value suffices, so the answer is 4.