AGC033 A - Darker and Darker
https://atcoder.jp/contests/agc033/tasks/agc033_a
提出
code: python
from collections import deque
h, w = map(int,input().split())
a = input() for _ in range(h)
black = []
for i in range(h):
for j in range(w):
if aij == "#":
black.append(i, j)
def dfs(q):
nowi, nowj = q.popleft()
# 多点スタート
for i in black:
q = deque(i)
dfs(q)
解答
code: python
from collections import deque
H, W = map(int, input().split())
grid = input() for _ in range(H)
dist = [-1 * W for _ in range(H)]
black_cells = deque()
for h in range(H):
for w in range(W):
if gridhw == '#':
black_cells.append((h, w))
disthw = 0
while black_cells:
h, w = black_cells.popleft()
d = disthw
for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
new_h = h + dy
new_w = w + dx
if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
continue
if distnew_hnew_w == -1:
distnew_hnew_w = d + 1
black_cells.append((new_h, new_w))
ans = -1
for i in range(H):
for j in range(W):
res = distij
if (res > ans):
ans = res
print(ans)
テーマ
#bfs
蟻本 2-1 迷路の最短路
メモ
【AtCoder】典型的な BFS 問題を解く【A - Darker and Darker】
AtCoder AGC 033 A - Darker and Darker (緑色, 300 点)