結果

問題 No.1572 XI
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-06-27 14:59:28
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,780 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 86,860 KB
実行使用メモリ 126,324 KB
最終ジャッジ日時 2023-09-07 17:57:31
合計ジャッジ時間 5,924 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
75,948 KB
testcase_01 AC 92 ms
71,272 KB
testcase_02 AC 93 ms
71,364 KB
testcase_03 AC 97 ms
71,476 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 507 ms
85,104 KB
testcase_08 TLE -
testcase_09 AC 1,319 ms
116,348 KB
testcase_10 TLE -
testcase_11 AC 1,020 ms
92,964 KB
testcase_12 TLE -
testcase_13 AC 1,123 ms
95,756 KB
testcase_14 AC 1,189 ms
108,572 KB
testcase_15 AC 1,122 ms
96,000 KB
testcase_16 AC 1,908 ms
118,604 KB
testcase_17 AC 435 ms
82,928 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

https://yukicoder.me/problems/no/1572

サイコロの向きは、6通りしかない
頑張って推移を書けばおk

0 表
1 上
2 右
3 下
4 左
5 裏

"""

import sys
from sys import stdin
from collections import deque

def encode(x,y,state):
    return H*W*state + y*H + x

def decode(now):
    state = now // (H*W)
    y = (now % (H*W)) // H
    x = (now % (H*W)) % H
    return x,y,state

def chdi(state,movedir):

    if state == 0:
        if movedir == "U":
            return 1
        elif movedir == "R":
            return 2
        elif movedir == "D":
            return 3
        else:
            return 4

    elif state == 1: #上
        if movedir == "U":
            return 5
        elif movedir == "D":
            return 0
        else:
            return 1

    elif state == 2: #右
        if movedir == "R":
            return 5
        elif movedir == "L":
            return 0
        else:
            return 2

    elif state == 3: #下
        if movedir == "D":
            return 5
        elif movedir == "U":
            return 0
        else:
            return 3

    elif state == 4: #左
        if movedir == "L":
            return 5
        elif movedir == "R":
            return 0
        else:
            return 4

    else: #裏
        if movedir == "U":
            return 3
        elif movedir == "R":
            return 4
        elif movedir == "D":
            return 1
        else:
            return 2
    

H,W = map(int,stdin.readline().split())

sx,sy = map(int,stdin.readline().split())
gx,gy = map(int,stdin.readline().split())
sx -= 1
sy -= 1
gx -= 1
gy -= 1

A = [ list(stdin.readline()[:-1]) for i in range(H) ]
d = [float("inf")] * (H*W*6)

q = deque([encode(sx,sy,0)])
d[encode(sx,sy,0)] = 0

while q:

    code = q.popleft()
    x,y,state = decode(code)

    if x != 0 and A[x-1][y] != "#":
        nx,ny,nstate = x-1,y,chdi(state,"U")
        ncode = encode(nx,ny,nstate)

        if d[ncode] > d[code] + 1:
            d[ncode] = d[code] + 1
            q.append(ncode)

    if x != H-1 and A[x+1][y] != "#":
        nx,ny,nstate = x+1,y,chdi(state,"D")
        ncode = encode(nx,ny,nstate)

        if d[ncode] > d[code] + 1:
            d[ncode] = d[code] + 1
            q.append(ncode)

    if y != 0 and A[x][y-1] != "#":
        nx,ny,nstate = x,y-1,chdi(state,"L")
        ncode = encode(nx,ny,nstate)

        if d[ncode] > d[code] + 1:
            d[ncode] = d[code] + 1
            q.append(ncode)

    if y != W-1 and A[x][y+1] != "#":
        nx,ny,nstate = x,y+1,chdi(state,"R")
        ncode = encode(nx,ny,nstate)

        if d[ncode] > d[code] + 1:
            d[ncode] = d[code] + 1
            q.append(ncode)

ans = d[encode(gx,gy,0)]
print (ans if ans != float("inf") else -1)
0