結果

問題 No.367 ナイトの転身
ユーザー chocoruskchocorusk
提出日時 2020-09-26 04:03:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,371 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 13,656 KB
最終ジャッジ日時 2023-09-10 22:21:26
合計ジャッジ時間 7,238 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
8,788 KB
testcase_01 AC 19 ms
8,636 KB
testcase_02 AC 20 ms
8,700 KB
testcase_03 AC 19 ms
8,648 KB
testcase_04 AC 19 ms
8,704 KB
testcase_05 AC 20 ms
8,648 KB
testcase_06 AC 20 ms
8,632 KB
testcase_07 AC 20 ms
8,760 KB
testcase_08 AC 20 ms
8,696 KB
testcase_09 AC 19 ms
8,788 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

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