結果

問題 No.367 ナイトの転身
ユーザー tookunn_1213tookunn_1213
提出日時 2016-04-30 01:01:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,286 ms / 2,000 ms
コード長 1,098 bytes
コンパイル時間 408 ms
コンパイル使用メモリ 82,188 KB
実行使用メモリ 108,120 KB
最終ジャッジ日時 2024-04-15 08:00:24
合計ジャッジ時間 6,042 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,000 KB
testcase_01 AC 39 ms
54,212 KB
testcase_02 AC 39 ms
53,468 KB
testcase_03 AC 40 ms
54,696 KB
testcase_04 AC 39 ms
53,080 KB
testcase_05 AC 40 ms
53,344 KB
testcase_06 AC 47 ms
56,528 KB
testcase_07 AC 39 ms
53,016 KB
testcase_08 AC 39 ms
53,096 KB
testcase_09 AC 39 ms
53,512 KB
testcase_10 AC 706 ms
102,664 KB
testcase_11 AC 1,286 ms
108,120 KB
testcase_12 AC 197 ms
85,584 KB
testcase_13 AC 228 ms
86,372 KB
testcase_14 AC 416 ms
86,548 KB
testcase_15 AC 107 ms
77,088 KB
testcase_16 AC 411 ms
86,348 KB
testcase_17 AC 158 ms
78,580 KB
testcase_18 AC 161 ms
78,784 KB
testcase_19 AC 171 ms
79,488 KB
testcase_20 AC 112 ms
78,148 KB
testcase_21 AC 167 ms
79,392 KB
testcase_22 AC 40 ms
53,624 KB
testcase_23 AC 71 ms
72,780 KB
testcase_24 AC 95 ms
76,980 KB
testcase_25 AC 97 ms
77,084 KB
testcase_26 AC 72 ms
70,268 KB
権限があれば一括ダウンロードができます

ソースコード

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