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()