結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-04-30 00:38:17 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,226 ms / 2,000 ms |
| コード長 | 1,364 bytes |
| コンパイル時間 | 161 ms |
| コンパイル使用メモリ | 82,336 KB |
| 実行使用メモリ | 121,336 KB |
| 最終ジャッジ日時 | 2024-10-04 23:39:26 |
| 合計ジャッジ時間 | 7,043 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
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)