結果

問題 No.367 ナイトの転身
ユーザー roiti46roiti46
提出日時 2016-04-30 00:38:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,466 ms / 2,000 ms
コード長 1,364 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 82,524 KB
実行使用メモリ 121,344 KB
最終ジャッジ日時 2024-04-15 07:57:29
合計ジャッジ時間 8,291 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
67,936 KB
testcase_01 AC 64 ms
67,944 KB
testcase_02 AC 64 ms
67,876 KB
testcase_03 AC 64 ms
69,124 KB
testcase_04 AC 65 ms
68,696 KB
testcase_05 AC 66 ms
69,144 KB
testcase_06 AC 74 ms
70,624 KB
testcase_07 AC 63 ms
67,524 KB
testcase_08 AC 64 ms
68,044 KB
testcase_09 AC 64 ms
68,560 KB
testcase_10 AC 963 ms
112,472 KB
testcase_11 AC 1,466 ms
121,344 KB
testcase_12 AC 350 ms
91,004 KB
testcase_13 AC 326 ms
90,996 KB
testcase_14 AC 603 ms
93,968 KB
testcase_15 AC 202 ms
81,772 KB
testcase_16 AC 593 ms
94,060 KB
testcase_17 AC 270 ms
81,864 KB
testcase_18 AC 303 ms
83,560 KB
testcase_19 AC 301 ms
85,152 KB
testcase_20 AC 205 ms
82,668 KB
testcase_21 AC 306 ms
83,988 KB
testcase_22 AC 71 ms
70,700 KB
testcase_23 AC 111 ms
79,472 KB
testcase_24 AC 180 ms
80,932 KB
testcase_25 AC 149 ms
79,592 KB
testcase_26 AC 116 ms
79,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue
H,W = map(int,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 range(2)]for j in range(W)] for i in range(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 = queue.Queue()
q.put(0)
q.put(sy)
q.put(sx)
q.put(True)
used[sy][sx][0] = True
while q.qsize() > 0 and (not find):
    c = q.get()
    y = q.get()
    x = q.get()
    isKnight = q.get()
    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
        q.put(c + 1)
        q.put(ny)
        q.put(nx)
        q.put(mode)
if not find:
    print (-1)
0