結果

問題 No.367 ナイトの転身
ユーザー zombietanzombietan
提出日時 2016-06-19 15:54:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 1,873 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 95,768 KB
最終ジャッジ日時 2024-04-19 13:00:17
合計ジャッジ時間 2,951 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,888 KB
testcase_01 AC 35 ms
54,144 KB
testcase_02 AC 37 ms
54,144 KB
testcase_03 AC 36 ms
53,888 KB
testcase_04 AC 35 ms
54,016 KB
testcase_05 AC 35 ms
53,760 KB
testcase_06 AC 42 ms
56,832 KB
testcase_07 AC 37 ms
53,760 KB
testcase_08 AC 35 ms
53,760 KB
testcase_09 AC 38 ms
53,888 KB
testcase_10 AC 163 ms
93,000 KB
testcase_11 AC 198 ms
95,768 KB
testcase_12 AC 109 ms
82,508 KB
testcase_13 AC 105 ms
82,432 KB
testcase_14 AC 131 ms
83,564 KB
testcase_15 AC 74 ms
77,084 KB
testcase_16 AC 137 ms
82,808 KB
testcase_17 AC 91 ms
77,056 KB
testcase_18 AC 98 ms
77,528 KB
testcase_19 AC 89 ms
77,952 KB
testcase_20 AC 68 ms
74,880 KB
testcase_21 AC 98 ms
78,084 KB
testcase_22 AC 43 ms
54,528 KB
testcase_23 AC 57 ms
66,048 KB
testcase_24 AC 71 ms
73,216 KB
testcase_25 AC 69 ms
72,064 KB
testcase_26 AC 52 ms
63,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
H, W = map(int, input().split())
board = [list(input()) for _ in range(H)]

n_move = [(-2, -1),(-2, 1),(-1, -2),(-1, 2),(1, -2),(1, 2),(2, -1),(2, 1)]
b_move = [(-1, 1),(-1, -1),(1, 1),(1, -1)]

n_reached = [[0 for _ in range(W)] for __ in range(H)]
b_reached = [[0 for _ in range(W)] for __ in range(H)]

for x in range(W):
    for y in range(H):
        if board[y][x] == 'S':
            start = (x, y)

class Piece:
    def __init__(self, state, style, step):
        self.state = state
        self.style = style
        self.step = step

def BFS(s):
    q = deque()
    q.append(Piece(s, 'knight', 0))
    while q:
        current = q.popleft()
        x, y = current.state
        form = current.style
        count = current.step
        if board[y][x] == 'G':
            return count
        if form == 'knight':
            for j, k in n_move:
                nx, ny = x + j, y + k
                if 0 > nx or nx > W-1 or 0 > ny or ny > H-1:
                    continue
                if n_reached[ny][nx] == 1:
                    continue
                n_reached[ny][nx] = 1
                if board[ny][nx] == 'R':
                    nform = 'bishop'
                else:
                    nform = 'knight'
                p = Piece((nx, ny), nform, count+1)
                q.append(p)
        else:
            for n, m in b_move:
                nx, ny = x + n, y + m
                if 0 > nx or nx > W-1 or 0 > ny or ny > H-1:
                    continue
                if b_reached[ny][nx] == 1:
                    continue
                b_reached[ny][nx] = 1
                if board[ny][nx] == 'R':
                    nform = 'knight'
                else:
                    nform = 'bishop'
                p = Piece((nx, ny), nform, count+1)
                q.append(p)
    return -1

print(BFS(start))
0