Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
# -*- coding: utf-8 -*-
from itertools import permutations
N, C = map(int, input().split())
D = [int(_) for c in range(C) for _ in input().split()]

table = [0] * C * 3
for i, g in enumerate([int(_) for n in range(N) for _ in input().split()]):
    for c in range(C):
        table[i % 3 * C + c] += D[C * (g - 1) + c]


def cost(comb):
    return table[C * 0 + comb[0]] + table[C * 1 + comb[1]] + table[C * 2
                                                                   + comb[2]]


combs = permutations(range(C), 3)
min_cst = cost(min(combs, key=lambda comb: cost(comb)))
print(min_cst)


# -*- coding: utf-8 -*-
from itertools import permutations
N, C = map(int, input().split())
D = [int(_) for c in range(C) for _ in input().split()]
G = [int(_) for n in range(N) for _ in input().split()]

table = [0] * C * 3
for i in range(N * N):
    for c in range(C):
        table[i % 3 * C + c] += D[C * (G[i] - 1) + c]


def cost(comb):
    return table[C * 0 + comb[0]] + table[C * 1 + comb[1]] + table[C * 2
                                                                   + comb[2]]


combs = permutations(range(C), 3)
min_cst = cost(min(combs, key=lambda comb: cost(comb)))
print(min_cst)


# -*- coding: utf-8 -*-
# import functools

# @functools.lru_cache(maxsize=None)

#     cost = 0
#     for row in range(N):
#         for col in range(N):
#             if (row + col) % 3 == i:
#                 cost += D[G[row][col] - 1][c - 1]

#     return cost


#     cost = 0
#     for i, c in enumerate(comb):
#         cost += paint(i, c)

#     return cost


def cost(comb):
    cost = 0
    for r in range(N):
        for c in range(N):
            if (r + c) % 3 == 0:
                cost += D[G[r][c] - 1][comb[0]]
            elif (r + c) % 3 == 1:
                cost += D[G[r][c] - 1][comb[1]]
            else:
                cost += D[G[r][c] - 1][comb[2]]


if __name__ == '__main__':
    from itertools import permutations
    N, C = map(int, input().split())
    D = [[int(_) for _ in input().split()] for i in range(C)]
    G = [[int(_) for _ in input().split()] for i in range(N)]
    combs = permutations(range(1, C + 1), 3)
    min_cost = min([cost(comb) for comb in combs])
    print(min_cost)


# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted