結果

問題 No.367 ナイトの転身
ユーザー roiti46roiti46
提出日時 2016-04-30 00:37:39
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 1,317 ms / 2,000 ms
コード長 1,324 bytes
コンパイル時間 255 ms
コンパイル使用メモリ 76,988 KB
実行使用メモリ 111,160 KB
最終ジャッジ日時 2024-04-15 07:56:43
合計ジャッジ時間 8,600 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
75,528 KB
testcase_01 AC 75 ms
75,380 KB
testcase_02 AC 76 ms
75,268 KB
testcase_03 AC 76 ms
75,332 KB
testcase_04 AC 76 ms
75,416 KB
testcase_05 AC 77 ms
75,660 KB
testcase_06 AC 84 ms
76,280 KB
testcase_07 AC 78 ms
75,320 KB
testcase_08 AC 76 ms
75,704 KB
testcase_09 AC 79 ms
75,688 KB
testcase_10 AC 722 ms
104,592 KB
testcase_11 AC 1,317 ms
111,160 KB
testcase_12 AC 222 ms
89,020 KB
testcase_13 AC 247 ms
88,460 KB
testcase_14 AC 435 ms
89,452 KB
testcase_15 AC 136 ms
79,392 KB
testcase_16 AC 426 ms
88,864 KB
testcase_17 AC 195 ms
80,056 KB
testcase_18 AC 193 ms
81,020 KB
testcase_19 AC 199 ms
82,180 KB
testcase_20 AC 140 ms
79,888 KB
testcase_21 AC 194 ms
81,604 KB
testcase_22 AC 78 ms
75,820 KB
testcase_23 AC 106 ms
78,232 KB
testcase_24 AC 133 ms
79,164 KB
testcase_25 AC 134 ms
78,892 KB
testcase_26 AC 106 ms
78,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
H,W = map(int,raw_input().split())
s = [raw_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