Official
E - ブースの配置 Editorial
by
E - ブースの配置 Editorial
by
sounansya
ブースを置く \(3\) 箇所を全探索すれば良いです。
\(3\) 箇所を決め打ちした際にその配置が条件を満たすかどうかの判定はブースを始点とした BFS を行うことで可能です。
実装例(Python3)
from itertools import combinations
from collections import deque
h, w = map(int, input().split())
s = [input() for _ in range(h)]
valid = []
for i in range(h):
for j in range(w):
if s[i][j] == ".":
valid.append((i, j))
ans = 0
for c in combinations(valid, 3):
d = [[0] * w for i in range(h)]
q = deque()
for i in range(3):
x, y = c[i]
d[x][y] = 1 << i
q.append(c[i])
while q:
x0, y0 = q.popleft()
for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)):
x, y = x0 + dx, y0 + dy
if 0 <= x < h and 0 <= y < w and s[x][y] == "." and (x, y) not in c:
v = d[x][y] | d[x0][y0]
if d[x][y] == v:
continue
d[x][y] = v
q.append((x, y))
ok = False
for i in range(h):
for j in range(w):
ok |= d[i][j] == 7
ans += ok
print(ans)
posted:
last update:
