結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
71,648 KB
testcase_01 AC 100 ms
71,724 KB
testcase_02 AC 103 ms
71,748 KB
testcase_03 AC 100 ms
71,504 KB
testcase_04 AC 101 ms
71,736 KB
testcase_05 AC 100 ms
71,452 KB
testcase_06 AC 103 ms
71,720 KB
testcase_07 AC 102 ms
71,456 KB
testcase_08 AC 101 ms
71,868 KB
testcase_09 AC 101 ms
71,752 KB
testcase_10 AC 199 ms
82,924 KB
testcase_11 AC 272 ms
85,192 KB
testcase_12 AC 236 ms
81,356 KB
testcase_13 AC 199 ms
79,900 KB
testcase_14 AC 218 ms
81,480 KB
testcase_15 AC 153 ms
78,016 KB
testcase_16 AC 217 ms
79,444 KB
testcase_17 AC 161 ms
78,388 KB
testcase_18 AC 169 ms
78,588 KB
testcase_19 AC 174 ms
78,728 KB
testcase_20 AC 159 ms
78,552 KB
testcase_21 AC 177 ms
78,636 KB
testcase_22 AC 111 ms
77,188 KB
testcase_23 AC 141 ms
78,304 KB
testcase_24 AC 143 ms
78,172 KB
testcase_25 AC 140 ms
77,988 KB
testcase_26 AC 122 ms
77,324 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, t
                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, t
            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