結果

問題 No.2096 Rage With Our Friends
ユーザー mkawa2
提出日時 2022-10-07 22:57:50
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,353 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 849,864 KB
最終ジャッジ日時 2024-06-12 20:33:22
合計ジャッジ時間 11,019 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 WA * 2 MLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

from collections import deque

h, w = LI()
si, sj, gi, gj = LI1()
ss = [SI() for _ in range(h)]

mxs = [1]*w
d0 = [[inf]*h for _ in range(w)]
dd = [[inf]*2 for _ in range(h*w)]

q = deque()
for j in range(w):
    q.append((0, j, 0))
    d0[j][0] = 0
while q:
    i, j, f = q.popleft()
    if i == 0:
        d = d0[j][f]

        ni = min(1+f//2, h-1)
        for nj in [j-1, j+1]:
            if nj >= w or nj < 0: continue
            if d < dd[ni*w+nj][1]:
                dd[ni*w+nj][1] = d
                q.appendleft((ni, nj, 1))

        ni = min(1+f, h-1)
        for nj in [j-1, j+1]:
            if nj >= w or nj < 0: continue
            if d+1 < dd[ni*w+nj][1]:
                dd[ni*w+nj][1] = d+1
                q.append((ni, nj, 1))

    else:
        d = dd[i*w+j][f]
        if f:
            if ss[i][j] == "." and d < dd[i*w+j][0]:
                dd[i*w+j][0] = d
                q.appendleft((i, j, 0))
            if i > 1 and d < dd[(i-1)*w+j][1]:
                dd[(i-1)*w+j][1] = d
                q.appendleft((i-1, j, 1))
        else:
            ni = i+1
            if ni < h:
                for nj in [j-1, j+1]:
                    if nj < 0 or nj >= w: continue
                    if ss[ni][nj] == "." and d < dd[ni*w+nj][0]:
                        dd[ni*w+nj][0] = d
                        q.appendleft((ni, nj, 0))

            for nj in [j-1, j+1]:
                if nj < 0 or nj >= w: continue
                if d < d0[nj][i]:
                    d0[nj][i] = d
                    q.appendleft((0, nj, i))
# p2D(d0)
# p2D(dd)

ans = dd[gi*w+gj][0]
if ans == inf: ans = -1

print(ans)
0