結果

問題 No.367 ナイトの転身
コンテスト
ユーザー yaoshimax
提出日時 2016-05-07 17:48:47
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 540 ms / 2,000 ms
コード長 909 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 486 ms
コンパイル使用メモリ 77,396 KB
最終ジャッジ日時 2025-12-03 20:36:48
ジャッジサーバーID
(参考情報)
judge2 / judge5
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

H,W=map(int,raw_input().split())
B=[raw_input() for i in range(H)]
arrived=[[[-1 for i in range(2)] for j in range(W)] for k in range(H)]
state=[]
for i in range(H):
    for j in range(W):
        if B[i][j]=='S':
            state.append((0,0,i,j))
    
moves=[[(1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1),(-2,1),(-1,2)],[(1,1),(-1,1),(-1,-1),(1,-1)]]

while len(state)!=0:
    nextState=[]
    for (p,s,x,y) in state:
        if B[x][y]=='G':
            print p
            exit()
        if arrived[x][y][s]!=-1:
            continue
        arrived[x][y][s]=p
        for (dx,dy) in moves[s]:
            nx,ny=x+dx,y+dy
            if nx<0 or ny <0 or nx>=H or ny >= W:
                continue
            ns=s
            if B[nx][ny]=='R':
                ns=1-s
            if arrived[nx][ny][ns]!=-1:
                continue
            nextState.append((p+1,ns,nx,ny))
    state=nextState
print -1
0