結果

問題 No.2096 Rage With Our Friends
ユーザー 👑 AngrySadEightAngrySadEight
提出日時 2022-09-14 23:44:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,577 ms / 5,000 ms
コード長 2,458 bytes
コンパイル時間 641 ms
コンパイル使用メモリ 82,104 KB
実行使用メモリ 265,384 KB
最終ジャッジ日時 2024-12-15 12:35:24
合計ジャッジ時間 19,557 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,888 KB
testcase_01 AC 49 ms
54,400 KB
testcase_02 AC 46 ms
54,144 KB
testcase_03 AC 47 ms
54,016 KB
testcase_04 AC 46 ms
54,272 KB
testcase_05 AC 49 ms
54,272 KB
testcase_06 AC 46 ms
54,016 KB
testcase_07 AC 48 ms
54,144 KB
testcase_08 AC 49 ms
54,828 KB
testcase_09 AC 48 ms
54,912 KB
testcase_10 AC 51 ms
54,784 KB
testcase_11 AC 418 ms
178,304 KB
testcase_12 AC 364 ms
177,404 KB
testcase_13 AC 888 ms
205,264 KB
testcase_14 AC 1,253 ms
194,032 KB
testcase_15 AC 416 ms
178,556 KB
testcase_16 AC 49 ms
55,168 KB
testcase_17 AC 851 ms
210,932 KB
testcase_18 AC 50 ms
61,440 KB
testcase_19 AC 46 ms
55,040 KB
testcase_20 AC 67 ms
68,608 KB
testcase_21 AC 2,544 ms
260,312 KB
testcase_22 AC 2,577 ms
265,384 KB
testcase_23 AC 500 ms
89,188 KB
testcase_24 AC 1,792 ms
183,320 KB
testcase_25 AC 1,764 ms
251,860 KB
testcase_26 AC 1,058 ms
121,876 KB
testcase_27 AC 968 ms
121,260 KB
testcase_28 AC 146 ms
77,952 KB
testcase_29 AC 690 ms
100,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

H, W = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
sx -= 1
sy -= 1
gx -= 1
gy -= 1
S = []
for i in range(H):
    s = str(input())
    S.append(s)

calc_highest_x = [H - 1 for _ in range(W)]
highest_x = [[H - 1 for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            calc_highest_x[j] = i
        highest_x[i][j] = calc_highest_x[j]

INF = 1 << 24

isvisited = [[False for _ in range(W)] for _ in range(H)]
dist = [[INF for _ in range(W)] for _ in range(H)]
dq = deque()
dq.appendleft([sx, sy])
dist[sx][sy] = 0

while len(dq):
    px, py = dq.popleft()
    if px == gx and py == gy:
        print(dist[gx][gy])
        exit()
    if isvisited[px][py]:
        continue
    isvisited[px][py] = True

    if py - 1 >= 0:
        next_x = min(H - 1, px + 1)
        next_x = highest_x[next_x][py - 1]
        dq.appendleft([next_x, py - 1])
        dist[next_x][py - 1] = min(dist[next_x][py - 1], dist[px][py])
    
    if py + 1 < W:
        next_x = min(H - 1, px + 1)
        next_x = highest_x[next_x][py + 1]
        dq.appendleft([next_x, py + 1])
        dist[next_x][py + 1] = min(dist[next_x][py + 1], dist[px][py])

    
    if py - 2 >= 0:
        next_x = min(H - 1, px + 1)
        next_x = highest_x[next_x][py - 2]
        dq.append([next_x, py - 2])
        dist[next_x][py - 2] = min(dist[next_x][py - 2], dist[px][py] + 1)

    if py - 2 >= 0:
        diffs_x = -(highest_x[px][py - 1] - px)
        next_x = highest_x[px][py - 1] + 1 + diffs_x // 2
        next_x = highest_x[min(H - 1, next_x)][py - 2]
        dq.appendleft([next_x, py - 2])
        dist[next_x][py - 2] = min(dist[next_x][py - 2], dist[px][py])

    if py + 2 < W:
        next_x = min(H - 1, px + 1)
        next_x = highest_x[next_x][py + 2]
        dq.append([next_x, py + 2])
        dist[next_x][py + 2] = min(dist[next_x][py + 2], dist[px][py] + 1)

    if py + 2 < W:
        diffs_x = -(highest_x[px][py + 1] - px)
        next_x = highest_x[px][py + 1] + 1 + diffs_x // 2
        next_x = highest_x[min(H - 1, next_x)][py + 2]
        dq.appendleft([next_x, py + 2])
        dist[next_x][py + 2] = min(dist[next_x][py + 2], dist[px][py])

    if 0 <= py < W:
        next_x = min(H - 1, px + 1)
        next_x = highest_x[next_x][py]
        dq.append([next_x, py])
        dist[next_x][py] = min(dist[next_x][py], dist[px][py] + 1)

print(-1)
0