結果

問題 No.2096 Rage With Our Friends
ユーザー chineristACchineristAC
提出日時 2022-09-14 21:38:08
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,104 bytes
コンパイル時間 514 ms
コンパイル使用メモリ 86,824 KB
実行使用メモリ 235,736 KB
最終ジャッジ日時 2023-08-21 21:16:27
合計ジャッジ時間 26,371 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 94 ms
71,404 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 101 ms
72,236 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 3,405 ms
228,668 KB
testcase_23 WA -
testcase_24 AC 2,470 ms
177,120 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 1,074 ms
105,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


import sys
from collections import *

input = lambda :sys.stdin.readline().rstrip()

"""
考察の内容
01-BFSでよさそうだけどEを持たなきゃいけなさそう?
→前進するとE=0になるので後退→前進の2回分を全探索でよさげ

後退については一番後退せずにすむ点にジャンプするのが得(この後後退する場合もできるだけ前のほうが候補が増える)
前進についてはできるだけ前にだけいった後、好きなだけbackできるように頂点倍化?
"""

H,W = map(int,input().split())
_,sy,_,gy = map(int,input().split())
sy,gy = sy-1,gy-1
S = [input() for i in range(H)]

mini = [[H for j in range(W)] for i in range(H)]
for j in range(W):
    for i in range(H)[::-1]:
        if i!=H-1:
            mini[i][j] = min(mini[i][j],mini[i+1][j])
        if S[i][j]=="#":
            mini[i][j] = i

INF = 10**17

"""
p=1のとき同じ列でback可能
"""
dist = [[INF for j in range(W)] for i in range(H)]
dist[H-1][sy] = 0
deq = deque([(H-1,sy)])
done = [[False]*W for i in range(H)]
while deq:
    h,w = deq.popleft()
    if done[h][w]:
        continue
    done[h][w] = True


    """
    前進する
    """
    if 1:
        if w!=0 and mini[max(0,h-1)][w-1]!=H:
            nh = mini[max(0,h-1)][w-1]
            if dist[nh][w-1] > dist[h][w]:
                dist[nh][w-1] = dist[h][w]
                deq.appendleft((nh,w-1))
        if w!=W-1 and mini[max(0,h-1)][w+1]!=H:
            nh = mini[max(0,h-1)][w+1]
            if dist[nh][w+1] > dist[h][w]:
                dist[nh][w+1] = dist[h][w]
                deq.appendleft((nh,w+1))

    """
    後退する
    """
    if w!=0 and mini[h][w-1]!=H:
        nh,nw = mini[h][w-1],w-1
        
        E = nh-h
        """
        ここからさらに前進する
        """
        if nw!=0 and mini[max(0,nh-1-E//2)][nw-1]!=H:
            nnh,nnw = mini[max(0,nh-1-E//2)][nw-1],nw-1
            if dist[nnh][nnw] > dist[h][w]:
                dist[nnh][nnw] = dist[h][w]
                deq.appendleft((nnh,nnw))
        if nw!=W-1 and mini[max(0,nh-1-E//2)][nw+1]!=H:
            nnh,nnw = mini[max(0,nh-1-E//2)][nw+1],nw+1
            if dist[nnh][nnw] > dist[h][w]:
                dist[nnh][nnw] = dist[h][w]
                deq.appendleft((nnh,nnw))
        """
        ブースト
        """
        if nw!=0 and mini[max(0,nh-1-E)][nw-1]!=H:
            nnh,nnw = mini[max(0,nh-1-E)][nw-1],nw-1
            if dist[nnh][nnw] > dist[h][w] + 1:
                dist[nnh][nnw] = dist[h][w] + 1
                deq.append((nnh,nnw))
        if nw!=W-1 and mini[max(0,nh-1-E)][nw+1]!=H:
            nnh,nnw = mini[max(0,nh-1-E)][nw+1],nw+1
            if dist[nnh][nnw] > dist[h][w] + 1:
                dist[nnh][nnw] = dist[h][w] + 1
                deq.append((nnh,nnw))
        
        

    if w!=W-1 and mini[h][w+1]!=H:
        nh,nw = mini[h][w+1],w+1
        
        E = nh-h
        """
        ここからさらに前進する
        """
        if nw!=0 and mini[max(0,nh-1-E//2)][nw-1]!=H:
            nnh,nnw = mini[max(0,nh-1-E//2)][nw-1],nw-1
            if dist[nnh][nnw] > dist[h][w]:
                dist[nnh][nnw] = dist[h][w]
                deq.appendleft((nnh,nnw))
        if nw!=W-1 and mini[max(0,nh-1-E//2)][nw+1]!=H:
            nnh,nnw = mini[max(0,nh-1-E//2)][nw+1],nw+1
            if dist[nnh][nnw] > dist[h][w]:
                dist[nnh][nnw] = dist[h][w]
                deq.appendleft((nnh,nnw))
        """
        ブースト
        """
        if nw!=0 and mini[max(0,nh-1-E)][nw-1]!=H:
            nnh,nnw = mini[max(0,nh-1-E)][nw-1],nw-1
            if dist[nnh][nnw] > dist[h][w] + 1:
                dist[nnh][nnw] = dist[h][w] + 1
                deq.append((nnh,nnw))
        if nw!=W-1 and mini[max(0,nh-1-E)][nw+1]!=H:
            nnh,nnw = mini[max(0,nh-1-E)][nw+1],nw+1
            if dist[nnh][nnw] > dist[h][w] + 1:
                dist[nnh][nnw] = dist[h][w] + 1
                deq.append((nnh,nnw))
    
    

if dist[0][gy] == INF:
    print(-1)
else:
    print(dist[0][gy])

0