結果

問題 No.2096 Rage With Our Friends
ユーザー 👑 tamatotamato
提出日時 2022-10-07 22:48:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,486 ms / 5,000 ms
コード長 4,424 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 86,904 KB
実行使用メモリ 165,168 KB
最終ジャッジ日時 2023-09-03 14:29:23
合計ジャッジ時間 25,016 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
72,256 KB
testcase_01 AC 96 ms
72,316 KB
testcase_02 AC 99 ms
72,460 KB
testcase_03 AC 97 ms
72,408 KB
testcase_04 AC 99 ms
72,228 KB
testcase_05 AC 98 ms
72,236 KB
testcase_06 AC 98 ms
72,468 KB
testcase_07 AC 99 ms
72,248 KB
testcase_08 AC 103 ms
76,236 KB
testcase_09 AC 102 ms
76,432 KB
testcase_10 AC 101 ms
72,048 KB
testcase_11 AC 311 ms
115,696 KB
testcase_12 AC 511 ms
150,340 KB
testcase_13 AC 1,611 ms
127,776 KB
testcase_14 AC 1,290 ms
125,124 KB
testcase_15 AC 300 ms
115,780 KB
testcase_16 AC 104 ms
76,420 KB
testcase_17 AC 1,943 ms
147,364 KB
testcase_18 AC 108 ms
76,440 KB
testcase_19 AC 103 ms
76,404 KB
testcase_20 AC 107 ms
76,292 KB
testcase_21 AC 3,396 ms
158,496 KB
testcase_22 AC 3,486 ms
165,168 KB
testcase_23 AC 637 ms
89,720 KB
testcase_24 AC 2,579 ms
135,764 KB
testcase_25 AC 1,056 ms
110,020 KB
testcase_26 AC 1,286 ms
108,000 KB
testcase_27 AC 1,469 ms
111,564 KB
testcase_28 AC 161 ms
79,148 KB
testcase_29 AC 1,131 ms
99,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    from bisect import bisect_left
    from collections import deque
    input = sys.stdin.readline

    H, W = map(int, input().split())
    sh, sw, gh, gw = map(int, input().split())
    gh -= 1
    gw -= 1
    G = []
    for _ in range(H):
        G.append(input().rstrip('\n'))

    R = []
    for w in range(W):
        col = []
        for h in range(H):
            if G[h][w] == ".":
                col.append(h)
        R.append(col)

    que = deque()
    seen = [[-1] * W for _ in range(H)]
    for w in range(W):
        que.append((0, 0, w))
        seen[0][w] = 0
    while que:
        x, h, w = que.popleft()
        if seen[h][w] < x:
            continue
        #print(x, h, w)

        # normal jump
        if w - 1 >= 0:
            j = bisect_left(R[w - 1], h+2) - 1
            h_back = R[w - 1][j]
            if seen[h_back][w - 1] == -1 or seen[h_back][w - 1] > x:
                seen[h_back][w - 1] = x
                que.appendleft((x, h_back, w - 1))
                for j_ in range(j - 1, -1, -1):
                    h_ = R[w - 1][j_]
                    if seen[h_][w - 1] == -1 or seen[h_][w - 1] > x:
                        seen[h_][w - 1] = x
                    else:
                        break
            if w - 2 >= 0:
                h_new = h_back + max(0, h - h_back) // 2 + 1
                jj = bisect_left(R[w - 2], h_new + 1) - 1
                hh = R[w - 2][jj]
                if seen[hh][w - 2] == -1 or seen[hh][w - 2] > x:
                    seen[hh][w - 2] = x
                    que.appendleft((x, hh, w - 2))
                    for j_ in range(jj - 1, -1, -1):
                        h_ = R[w - 2][j_]
                        if seen[h_][w - 2] == -1 or seen[h_][w - 2] > x:
                            seen[h_][w - 2] = x
                        else:
                            break
        if w + 1 < W:
            j = bisect_left(R[w + 1], h+2) - 1
            h_back = R[w + 1][j]
            if seen[h_back][w + 1] == -1 or seen[h_back][w + 1] > x:
                seen[h_back][w + 1] = x
                que.appendleft((x, h_back, w + 1))
                for j_ in range(j - 1, -1, -1):
                    h_ = R[w + 1][j_]
                    if seen[h_][w + 1] == -1 or seen[h_][w + 1] > x:
                        seen[h_][w + 1] = x
                    else:
                        break
            if w + 2 < W:
                h_new = h_back + max(0, h - h_back) // 2 + 1
                jj = bisect_left(R[w + 2], h_new + 1) - 1
                hh = R[w + 2][jj]
                if seen[hh][w + 2] == -1 or seen[hh][w + 2] > x:
                    seen[hh][w + 2] = x
                    que.appendleft((x, hh, w + 2))
                    for j_ in range(jj - 1, -1, -1):
                        h_ = R[w + 2][j_]
                        if seen[h_][w + 2] == -1 or seen[h_][w + 2] > x:
                            seen[h_][w + 2] = x
                        else:
                            break

        # super jump
        if w - 2 >= 0:
            h_new = h + 1
            jj = bisect_left(R[w - 2], h_new + 1) - 1
            hh = R[w - 2][jj]
            if seen[hh][w - 2] == -1:
                seen[hh][w - 2] = x + 1
                que.append((x + 1, hh, w - 2))
                for j_ in range(jj - 1, -1, -1):
                    h_ = R[w - 2][j_]
                    if seen[h_][w - 2] == -1 or seen[h_][w - 2] > x + 1:
                        seen[h_][w - 2] = x + 1
                    else:
                        break
        if w + 2 < W:
            h_new = h + 1
            jj = bisect_left(R[w + 2], h_new + 1) - 1
            hh = R[w + 2][jj]
            if seen[hh][w + 2] == -1:
                seen[hh][w + 2] = x + 1
                que.append((x + 1, hh, w + 2))
                for j_ in range(jj - 1, -1, -1):
                    h_ = R[w + 2][j_]
                    if seen[h_][w + 2] == -1 or seen[h_][w + 2] > x + 1:
                        seen[h_][w + 2] = x + 1
                    else:
                        break
        h_new = h + 1
        if h_new < H:
            if G[h_new][w] == ".":
                if seen[h_new][w] == -1:
                    seen[h_new][w] = x + 1
                    que.append((x + 1, h_new, w))

    print(seen[gh][gw])
    #for h in range(H):
    #    print(*seen[h])


if __name__ == '__main__':
    main()
0