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