목차

불 끄기

ps
링크acmicpc.net/…
출처BOJ
문제 번호14939
문제명불 끄기
레벨플래티넘 5
분류

애드혹

시간복잡도O(2^n * n^2)
인풋사이즈n=10
사용한 언어Python
제출기록30860KB / 148ms
최고기록52ms
해결날짜2022/01/22

풀이

코드

"""Solution code for "BOJ 14939. 불 끄기".

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

Tags: [Ad Hoc]
"""

import itertools

SIZE = 10
ON = 'O'
OFF = '#'
DELTAS = ((0, 0), (-1, 0), (1, 0), (0, -1), (0, 1))


def press(grid, r, c):
    for dr, dc in DELTAS:
        nr, nc = r + dr, c + dc
        if 0 <= nr < SIZE and 0 <= nc < SIZE:
            grid[nr][nc] = ON if grid[nr][nc] == OFF else OFF


def main():
    init_grid = [input() for _ in range(SIZE)]

    for i in range(SIZE + 1):
        for cols in itertools.combinations(range(SIZE), i):
            grid = [list(row) for row in init_grid]
            count = i
            for c in cols:
                press(grid, 0, c)
            for r in range(1, SIZE):
                for c in range(SIZE):
                    if grid[r - 1][c] == ON:
                        press(grid, r, c)
                        count += 1
            if all(x == OFF for x in grid[-1]):
                print(count)
                return
    print('-1')


if __name__ == '__main__':
    main()