https://www.acmicpc.net/problem/17135
import copy
import sys
from collections import deque
from itertools import combinations
dx, dy = [-1, 0, 1], [0, -1, 0]
def pass_condition():
for y in range(n):
for x in range(m):
if temp_maps[y][x]:
return False
return True
def bfs(archers):
q = deque()
cnt = 0
visit = [[0] * m for _ in range(n + 1)]
hits = []
for i in archers:
q.append((n, i))
visit[n][i] = 0
while q:
y, x = q.popleft()
for i in range(3):
cy, cx = y + dy[i], x + dx[i]
if cy < 0 or cy >= n or cx < 0 or cx >= m:
continue
if temp_maps[cy][cx] and visit[y][x] + 1 <= d:
hits.append((cy, cx))
q.clear()
break
elif not temp_maps[cy][cx] and visit[y][x] + 1 <= d:
visit[cy][cx] = visit[y][x] + 1
q.append((cy, cx))
q.clear()
while hits:
y, x = hits.pop()
if temp_maps[y][x]:
temp_maps[y][x] = 0
cnt += 1
return cnt
def down():
for y in range(n - 1, -1, -1):
for x in range(m):
if y == n - 1:
temp_maps[y][x] = 0
else:
temp_maps[y + 1][x] = temp_maps[y][x]
if not y and temp_maps[y][x]:
temp_maps[y][x] = 0
n, m, d = map(int, sys.stdin.readline().split())
maps = []
temp_maps = None
for _ in range(n):
maps.append(list(map(int, sys.stdin.readline().split())))
maps.append([0] * m) # 성벽
archers_list = list(combinations(range(0, m), 3))
answer = 0
for archers in archers_list:
temp_maps = copy.deepcopy(maps)
summ = 0
while not pass_condition():
summ += bfs(archers)
down()
answer = max(summ, answer)
print(answer)
1년 전에 이 문제를 풀었을 때는 한 20번은 넘게 틀린 것 같은데, 오늘 2트만에 성공해 기분이 매우 좋은 상태 ㅎㅎ