結果

問題 No.367 ナイトの転身
ユーザー roiti46roiti46
提出日時 2016-04-30 00:37:14
言語 PyPy2
(7.3.15)
結果
RE  
実行時間 -
コード長 1,320 bytes
コンパイル時間 1,733 ms
コンパイル使用メモリ 76,340 KB
実行使用メモリ 75,928 KB
最終ジャッジ日時 2024-04-15 07:56:34
合計ジャッジ時間 4,691 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
H,W = map(int,raw_input().split())
s = [input() for i in range(H)]
sx,sy,gx,gy = 0,0,0,0
for i in range(H):
    for j in range(W):
        if s[i][j] == 'S':
            sy = i
            sx = j
        elif s[i][j] == 'G':
            gy = i
            gx = j
used = [[[False for k in xrange(2)]for j in xrange(W)] for i in xrange(H)]
find = False
adx = [2,-2,2,-2,1,-1,1,-1]
ady = [1,1,-1,-1,2,2,-2,-2]
bdx = [1,1,-1,-1]
bdy = [1,-1,1,-1]
q = [[0, sy, sx, True]]
used[sy][sx][0] = True
while q and (not find):
    ele = heapq.heappop(q)
    c = ele[0]
    y = ele[1]
    x = ele[2]
    isKnight =  ele[3]
    cdy = bdy
    cdx = bdx
    if isKnight:
        cdy = ady
        cdx = adx
    for i in range(len(cdx)):
        ny = cdy[i] + y
        nx = cdx[i] + x
        mode = isKnight
        if ny < 0 or nx < 0 or ny >= H or nx >= W:continue
        if isKnight:
            if used[ny][nx][0]:continue
        else:
            if used[ny][nx][1]:continue
        if ny == gy and nx == gx:
            print(c + 1)
            find = True
            break
        if s[ny][nx] == 'R':
            mode = (not mode)
        if isKnight:
            used[ny][nx][0] = True
        else:
            used[ny][nx][1] = True
        heapq.heappush(q, [c + 1, ny, nx, mode])
if not find:
    print (-1)
0