結果

問題 No.367 ナイトの転身
ユーザー tookunn_1213tookunn_1213
提出日時 2016-04-30 01:02:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,098 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 44,196 KB
最終ジャッジ日時 2024-04-15 08:00:37
合計ジャッジ時間 6,077 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import heapq
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 = []
heapq.heappush(q,[0,sy,sx,True]) 
used[sy][sx][0] = True
while len(q) > 0 and (not find):
	hq = heapq.heappop(q)
	c = hq[0]
	y = hq[1]
	x = hq[2]
	isKnight = hq[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