https://www.acmicpc.net/problem/15683
def dfs(i):
global min_size
if i == len(cctv):
min_size = min(min_size, get_size())
return
for d in range(4):
dirs[i] = d
dfs(i+1)
def get_size():
new_board = [line[:] for line in board]
for i in range(len(cctv)):
set_board(new_board, i)
return sum([line.count(0) for line in new_board])
def set_board(board, i):
y, x, num = cctv[i]
d = dirs[i]
if num == 1:
set_line(board, y, x, d)
elif num == 2:
set_line(board, y, x, d)
set_line(board, y, x, (d+2)%4)
elif num == 3:
set_line(board, y, x, d)
set_line(board, y, x, (d+1)%4)
elif num == 4:
set_line(board, y, x, d)
set_line(board, y, x, (d+1)%4)
set_line(board, y, x, (d+2)%4)
elif num == 5:
for j in range(4):
set_line(board, y, x, j)
def set_line(board, y, x, d):
while True:
y, x = y + dy[d], x + dx[d]
if 0 <= y < h and 0 <= x < w:
if board[y][x] == 0:
board[y][x] = 7
elif board[y][x] == 6:
break
else:
break
# init
import sys
read = sys.stdin.readline
h, w = map(int, read().split())
board = [list(map(int, read().split())) for _ in range(h)]
cctv = []
for i in range(h):
for j in range(w):
if 1 <= board[i][j] <= 5:
cctv.append([i, j, board[i][j]])
visited = [False] * len(cctv)
dirs = [-1] * len(cctv)
min_size = float('inf')
dy, dx = [-1, 0, 1, 0], [0, 1, 0, -1] # 북,동,남,서
# start
dfs(0)
print(min_size)