結果

問題 No.367 ナイトの転身
ユーザー chocoruskchocorusk
提出日時 2020-09-26 04:03:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,371 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 85,360 KB
最終ジャッジ日時 2023-09-10 22:21:42
合計ジャッジ時間 6,812 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 101 ms
71,560 KB
testcase_01 AC 99 ms
71,568 KB
testcase_02 AC 101 ms
71,808 KB
testcase_03 AC 101 ms
71,828 KB
testcase_04 AC 102 ms
71,596 KB
testcase_05 AC 102 ms
71,396 KB
testcase_06 AC 101 ms
71,836 KB
testcase_07 AC 101 ms
71,560 KB
testcase_08 AC 101 ms
71,568 KB
testcase_09 AC 100 ms
71,592 KB
testcase_10 AC 205 ms
83,004 KB
testcase_11 AC 263 ms
85,360 KB
testcase_12 AC 210 ms
79,968 KB
testcase_13 AC 178 ms
79,548 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 202 ms
79,740 KB
testcase_17 AC 152 ms
78,172 KB
testcase_18 AC 161 ms
78,080 KB
testcase_19 AC 165 ms
78,696 KB
testcase_20 AC 141 ms
78,500 KB
testcase_21 AC 171 ms
78,612 KB
testcase_22 AC 112 ms
77,068 KB
testcase_23 AC 128 ms
77,512 KB
testcase_24 AC 139 ms
78,164 KB
testcase_25 AC 134 ms
78,204 KB
testcase_26 AC 117 ms
77,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines

h, w=map(int, readline().split())
s=read().decode().split()
sx, sy, gx, gy=0, 0, 0, 0
for i, si in enumerate(s):
    j=si.find('S')
    if j>=0:sx, sy=i, j
    j=si.find('G')
    if j>=0:gx, gy=i, j
INF=10**9
d=[[INF]*(h*w) for _ in range(2)]
d[0][sx*w+sy]=0
from collections import deque
que=deque()
que.append((sx*w+sy, 0))
dn=[(1, 2), (2, 1)]
db=[(1, 1), (1, -1), (-1, 1), (-1, -1)]
while que:
    p, t=que.popleft()
    x, y=divmod(p, w)
    if t==0:
        for dx, dy in dn:
            for px, py in db:
                x1, y1, t1=x+dx*px, y+dy*py, 0
                if x1<0 or x1>=h or y1<0 or y1>=w:
                    continue
                if s[x1][y1]=='R':
                    t1=1
                p1=x1*w+y1
                if d[t1][p1]>d[t][p]+1:
                    d[t1][p1]=d[t][p]+1
                    que.append((p1, t1))
    else:
        for dx, dy in db:
            x1, y1, t1=x+dx, y+dy, 0
            if x1<0 or x1>=h or y1<0 or y1>=w:
                continue
            if s[x1][y1]=='R':
                t1=0
            p1=x1*w+y1
            if d[t1][p1]>d[t][p]+1:
                d[t1][p1]=d[t][p]+1
                que.append((p1, t1))
ans=min(d[0][gx*w+gy], d[1][gx*w+gy])
if ans==INF:
    print(-1)
else:
    print(ans)
0