(BOJ17281 ⚾

Link copied to clipboard

문제 설명

Link copied to clipboard
  • 야구 규칙을 따라 구현하는 문제
    • 모든 경우의 수를 탐색하는 브루트 포스가 필요하고,
    • 각 경우 탐색 과정도 몇가지 유의사항만 따르면 된다.

문제 풀이

Link copied to clipboard
def make_turn(turn: tuple[int]) -> list[int]:
    turn = list(turn[:3]) + [0] + list(turn[3:])
    return turn


def simulate(N: int, hitters: list[list[int]], turn: list[int]) -> int:
    idx = score = 0
    for inning in range(N):
        out = 0
        first = second = third = 0
        while out < 3:
            hitter = turn[idx]
            point = hitters[inning][hitter]
            if point == 0:
                out += 1
            elif point == 1:
                score += third
                first, second, third = 1, first, second
            elif point == 2:
                score += second + third
                first, second, third = 0, 1, first
            elif point == 3:
                score += first + second + third
                first, second, third = 0, 0, 1
            elif point == 4:
                score += 1 + first + second + third
                first = second = third = 0
            idx = (idx + 1) % 9
    return score


if __name__ == "__main__":
    N = int(input())
    hitters = [list(map(int, input().split())) for _ in range(N)]
    max_score = 0
    for perm in permutations(range(1, 9), 8):
        score = simulate(N, hitters, make_turn(perm))
        if max_score < score:
            max_score = score
    print(max_score)
  • itertools.permuations()를 활용하여 모든 경우의 수를 구하고,
    • 1번 타자가 4번째 순번에 오도록 조정한다.
  • 구현 과정에서 각 이닝별 진행상황과 타자 번호 등을 잘 확인하면 된다.