목차

동전 게임

ps
링크acmicpc.net/…
출처BOJ
문제 번호9079
문제명동전 게임
레벨실버 4
분류

BFS

시간복잡도O(T)
인풋사이즈T<=10
사용한 언어Python
제출기록33680KB / 156ms
최고기록60ms
해결날짜2022/01/21

풀이

코드

"""Solution code for "BOJ 9079. 동전 게임".

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

Tags: [BFS]
"""

from teflib import search

FLIP_MASKS = (0b111000000, 0b000111000, 0b000000111, 0b100100100, 0b010010010,
              0b001001001, 0b100010001, 0b001010100)
INF = float('inf')


def main():
    dists = search.min_distances(lambda x: [x ^ m for m in FLIP_MASKS],
                                 0b000000000)
    T = int(input())
    for _ in range(T):
        board = [input() for _ in range(3)]
        coins = ' '.join(board).split()
        state = sum(1 << i for i, ch in enumerate(coins) if ch == 'H')
        flipped_state = state ^ 0b111111111
        answer = min(dists.get(state, INF), dists.get(flipped_state, INF))
        print('-1' if answer == INF else answer)


if __name__ == '__main__':
    main()