목차

피아의 아틀리에 ~신비한 대회의 연금술사~

ps
링크acmicpc.net/…
출처BOJ
문제 번호15898
문제명피아의 아틀리에 ~신비한 대회의 연금술사~
레벨골드 1
분류

구현

시간복잡도O(n^3 * 16^3)
인풋사이즈n<=10
사용한 언어Python 3.13
제출기록34536KB / 2584ms
최고기록2584ms
해결날짜2025/02/23

풀이

코드

"""Solution code for "BOJ 15898. 피아의 아틀리에 ~신비한 대회의 연금술사~".

- Problem link: https://www.acmicpc.net/problem/15898
- Solution link: http://www.teferi.net/ps/problems/boj/15898

Tags: [implementation]
"""

import itertools
import math

SCORE_BY_COLOR = {'R': 7, 'B': 5, 'G': 3, 'Y': 2, 'W': 0}
OFFSETS = [0, 1, 5, 6]


def create_rotated_arrays(mat):
    seqs = []
    for _ in range(4):
        mat = list(zip(*reversed(mat)))
        seq = []
        for row in mat:
            seq.extend(row)
            seq.append(0)
        seq.pop()
        seqs.append(seq)
    return seqs


def apply(e_cur, c_cur, ingre, offset):
    e_next, c_next = e_cur[:], c_cur[:]
    ingre_e, ingre_c = ingre
    for i, x in enumerate(ingre_e, start=offset):
        e = e_next[i] + x
        e_next[i] = 0 if e < 0 else (9 if e > 9 else e)
    for i, x in enumerate(ingre_c, start=offset):
        if x > 0:
            c_next[i] = x
    return e_next, c_next


def main():
    n = int(input())
    ingre_arrs = []
    for _ in range(n):
        effect_mat = [[int(x) for x in input().split()] for _ in range(4)]
        color_mat = [input().split() for _ in range(4)]

        color_mat = [[SCORE_BY_COLOR[c] for c in row] for row in color_mat]
        e_arrs = create_rotated_arrays(effect_mat)
        c_arrs = create_rotated_arrays(color_mat)
        ingre_arrs.append(list(zip(e_arrs, c_arrs)))

    answer = 0
    e0 = c0 = [0] * 25
    for ingre1, ingre2, ingre3 in itertools.permutations(ingre_arrs, 3):
        for i1 in ingre1:
            e1, c1 = apply(e0, c0, i1, 0)
            for i2, o2 in itertools.product(ingre2, OFFSETS):
                e2, c2 = apply(e1, c1, i2, o2)
                for i3, o3 in itertools.product(ingre3, OFFSETS):
                    e3, c3 = apply(e2, c2, i3, o3)
                    if (quality := math.sumprod(e3, c3)) > answer:
                        answer = quality

    print(answer)


if __name__ == '__main__':
    main()