結果

問題 No.2096 Rage With Our Friends
ユーザー chineristACchineristAC
提出日時 2022-09-15 00:05:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,605 ms / 5,000 ms
コード長 4,147 bytes
コンパイル時間 465 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 206,636 KB
最終ジャッジ日時 2023-08-21 21:36:06
合計ジャッジ時間 22,624 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,748 KB
testcase_01 AC 95 ms
71,564 KB
testcase_02 AC 95 ms
71,688 KB
testcase_03 AC 95 ms
71,660 KB
testcase_04 AC 99 ms
71,724 KB
testcase_05 AC 101 ms
71,736 KB
testcase_06 AC 100 ms
71,660 KB
testcase_07 AC 100 ms
71,740 KB
testcase_08 AC 107 ms
72,252 KB
testcase_09 AC 106 ms
72,148 KB
testcase_10 AC 106 ms
72,248 KB
testcase_11 AC 564 ms
177,740 KB
testcase_12 AC 1,348 ms
180,184 KB
testcase_13 AC 793 ms
179,164 KB
testcase_14 AC 1,042 ms
179,436 KB
testcase_15 AC 568 ms
177,596 KB
testcase_16 AC 100 ms
72,248 KB
testcase_17 AC 773 ms
186,892 KB
testcase_18 AC 104 ms
71,980 KB
testcase_19 AC 101 ms
72,284 KB
testcase_20 AC 104 ms
72,252 KB
testcase_21 AC 2,465 ms
205,868 KB
testcase_22 AC 2,605 ms
206,636 KB
testcase_23 AC 625 ms
90,488 KB
testcase_24 AC 1,892 ms
163,284 KB
testcase_25 AC 1,738 ms
136,616 KB
testcase_26 AC 1,276 ms
115,656 KB
testcase_27 AC 1,151 ms
116,220 KB
testcase_28 AC 203 ms
79,404 KB
testcase_29 AC 861 ms
100,248 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)]

maxi = [[-1 for j in range(W)] for i in range(H)]
for j in range(W):
    for i in range(H):
        if i!=0:
            maxi[i][j] = max(maxi[i][j],maxi[i-1][j])
        if S[i][j]==".":
            maxi[i][j] = i

INF = 10**17
"""
p=1のとき同じ列でback可能
"""
dist = [[INF for j in range(W)] for i in range(H)]
dist[0][sy] = 0
deq = deque([(0,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 maxi[min(H-1,h+1)][w-1]!=-1:
            nh = maxi[min(H-1,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 maxi[min(H-1,h+1)][w+1]!=-1:
            nh = maxi[min(H-1,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 maxi[h][w-1]!=-1:
        nh,nw = maxi[h][w-1],w-1
        
        E = h-nh
        """
        ここからさらに前進する
        """
        if nw!=0 and maxi[min(H-1,nh+1+E//2)][nw-1]!=-1:
            nnh,nnw = maxi[min(H-1,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 maxi[min(H-1,nh+1+E//2)][nw+1]!=-1:
            nnh,nnw = maxi[min(H-1,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 maxi[min(H-1,nh+1+E)][nw-1]!=-1:
            nnh,nnw = maxi[min(H-1,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 maxi[min(H-1,nh+1+E)][nw+1]!=-1:
            nnh,nnw = maxi[min(H-1,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 maxi[h][w+1]!=-1:
        nh,nw = maxi[h][w+1],w+1
        
        E = h-nh
        """
        ここからさらに前進する
        """
        if nw!=0 and maxi[min(H-1,nh+1+E//2)][nw-1]!=-1:
            nnh,nnw = maxi[min(H-1,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 maxi[min(H-1,nh+1+E//2)][nw+1]!=-1:
            nnh,nnw = maxi[min(H-1,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 maxi[min(H-1,nh+1+E)][nw-1]!=-1:
            nnh,nnw = maxi[min(H-1,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 maxi[min(H-1,nh+1+E)][nw+1]!=-1:
            nnh,nnw = maxi[min(H-1,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[H-1][gy] == INF:
    print(-1)
else:
    print(dist[H-1][gy])

0