Li Hua and Pattern (Codeforces 1797B)
Tags: Greedy, constructive algorithms

Li Hua has a pattern of size $$$n\times n$$$, each cell is either blue or red. He can perform exactly $$$k$$$ operations. In each operation, he chooses a cell and changes its color from red to blue or from blue to red. Each cell can be chosen as many times as he wants. Is it possible to make the pattern, that matches its rotation by $$$180^{\circ}$$$?Suppose you were Li Hua, please solve this problem.

Input: The first line contains the single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.The first line of each test case contains two integers $$$n,k$$$ ($$$1\le n\le 10^3,0\le k \le 10^9$$$) — the size of the pattern and the number of operations.Each of next $$$n$$$ lines contains $$$n$$$ integers $$$a_{i,j}$$$ ($$$a_{i,j}\in\{0,1\}$$$) — the initial color of the cell, $$$0$$$ for blue and $$$1$$$ for red.It's guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$.

Output: For each set of input, print "YES" if it's possible to make the pattern, that matches its rotation by $$$180^{\circ}$$$ after applying exactly $$$k$$$ of operations, and "NO" otherwise.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Note: In test case 1, you can't perform any operation. The pattern after rotation is on the right.  In test case 2, you can perform operations on $$$(2,1),(3,2),(3,4)$$$. The pattern after operations is in the middle and the pattern after rotation is on the right.  

Sample Inputs:
3
4 0
1 1 1 1
0 0 0 1
1 0 1 0
1 1 1 1
4 3
1 0 1 1
1 0 0 0
0 1 0 1
1 1 0 1
5 4
0 0 0 0 0
0 1 1 1 1
0 1 0 0 0
1 1 1 1 1
0 0 0 0 0

Sample Outputs:
NO
YES
YES

Solution (Python):
t = int(input())
for f in range(t):
    (n, k) = map(int, input().split())
    (l, res) = ([], 0)
    for x in range(n):
        l += [input().split()]
    for ind in range(n // 2):
        a = l[ind]
        b = l[-ind - 1]
        for i in range(n):
            if a[i] != b[-i - 1]:
                res += 1
    if n % 2:
        c = l[n // 2]
        for j in range(n // 2):
            if c[j] != c[-j - 1]:
                res += 1
    if res > k:
        print('NO')
    elif n % 2:
        print('YES')
    elif (k - res) % 2 == 0:
        print('YES')
    else:
        print('NO')

Solution (Python):
def main():
    t = int(input())
    for i in range(t):
        (n, k) = list(map(int, input().split()))
        pattern = []
        for j in range(n):
            pattern.append(list(map(int, input().split())))
        if n == 1:
            print('yes')
            continue
        count = 0
        for r in range(n // 2):
            for c in range(n):
                if pattern[r][c] != pattern[n - r - 1][n - c - 1]:
                    count += 1
        if n % 2:
            r = n // 2
            for c in range(n // 2):
                if pattern[r][c] != pattern[r][n - c - 1]:
                    count += 1
            if count <= k:
                print('yes')
            else:
                print('no')
        elif count <= k and (k - count) % 2 == 0:
            print('yes')
        else:
            print('no')
if __name__ == '__main__':
    main()

Solution (Python):
def rotate_matrix_180(matrix):
    new_matrix = [row[:] for row in matrix]
    for i in range(len(new_matrix)):
        new_matrix[i] = new_matrix[i][::-1]
    new_matrix = new_matrix[::-1]
    return new_matrix

def main():
    t = int(input())
    for i in range(t):
        (n, k) = input().split()
        n = int(n)
        k = int(k)
        t = []
        for j in range(n):
            row_input = input().split()
            t.append(row_input)
        t1 = rotate_matrix_180(t)
        t = [num for elem in t for num in elem]
        t1 = [num for elem in t1 for num in elem]
        need_flip = sum([1 for (x, y) in zip(t, t1) if x != y]) / 2
        if need_flip <= k and n % 2 == 1:
            print('YES')
        elif need_flip <= k and n % 2 == 0 and ((k - need_flip) % 2 == 0):
            print('YES')
        else:
            print('NO')
if __name__ == '__main__':
    main()