ABC007 C - 幅優先探索
https://atcoder.jp/contests/abc007/tasks/abc007_3
提出
code: python
from collections import deque
r, c = map(int, input().split())
sy, sx = map(int, input().split())
gy, gx = map(int, input().split())
maze = list(input()) for _ in range(r)
visited = list(0 for _ in range(c)) for _ in range(r)
que = deque(sy-1, sx-1, 1)
dis = 1
while (len(que) != 0):
nowY, nowX, dis = que.popleft()
if (nowX < 0 or nowX > c or nowY < 0 or nowY > r or mazenowYnowX == "#" or visitednowYnowX != 0):
continue
else:
visitednowYnowX += dis
dis += 1
que.append(nowY+1, nowX, dis)
que.append(nowY-1, nowX, dis)
que.append(nowY, nowX+1, dis)
que.append(nowY, nowX-1, dis)
print(visitedgy-1gx-1 - 1)
テーマ
#bfs
蟻本 2-1 迷路の最短路
メモ
code: python
from collections import deque
r, c = map(int, input().split())
si, sj = map(int, input().split())
gi, gj = map(int, input().split())
area = list(input()) for _ in range(r)
dis = list(0 for _ in range(c)) for _ in range(r)
que = deque(si-1, sj-1)
while que:
nowi, nowj = que.popleft()
for i, j in -1, 0], 0, 1, 0, -1, [1, 0:
nexti, nextj = nowi + i, nowj + j
if (nexti < 0 or nexti > r or nextj < 0 or nextj > c or areanextinextj == "#" or disnextinextj != 0):
continue
else:
disnextinextj = disnowinowj + 1
que.append(nexti, nextj)
print(disgi-1gj-1)