結果

問題 No.367 ナイトの転身
ユーザー nkn00bnkn00b
提出日時 2016-04-29 23:36:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,043 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 34,208 KB
最終ジャッジ日時 2024-04-15 06:05:01
合計ジャッジ時間 4,290 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
17,824 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 30 ms
10,880 KB
testcase_04 AC 31 ms
10,880 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 31 ms
10,880 KB
testcase_07 AC 31 ms
10,880 KB
testcase_08 AC 30 ms
10,880 KB
testcase_09 WA -
testcase_10 TLE -
testcase_11 -- -
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 #

def getstart(s):
	for i in range(len(s)):
		for j in range(len(s[i])):
			if s[i][j] == 'S':
				return (i, j, 0, 0)

def knight(w,h,i,j):
	for a in [-1, +1]:
		for b in [-2, +2]:
			na = i+a
			nb = j+b
			if (0 <= na < h) and (0 <= nb < w):
				yield (na, nb)
	for a in [-2, +2]:
		for b in [-1, +1]:
			na = i+a
			nb = j+b
			if (0 <= na < h) and (0 <= nb < w):
				yield (na, nb)

def bishop(w,h,i,j):
	for a in [-1, +1]:
		for b in [-1, +1]:
			na = i+a
			nb = j+b
			if (0 <= na < h) and (0 <= nb < w):
				yield (na, nb)

def main():
	h,w = map(int, input().split())
	s = [input() for i in range(h)]
	q = [getstart(s)]
	visited = set()
	while q:
		i,j,bn,d = q.pop(0)
		visited.add((i,j))
		if s[i][j] == 'G':
			print(d)
			return
		elif s[i][j] == 'R':
			bn += 1
		if bn % 2 == 0:
			for ni,nj in knight(w, h, i, j):
				if (ni,nj) not in visited:
					q.append((ni,nj,bn,d+1))
		else:
			for ni,nj in bishop(w, h, i, j):
				if (ni,nj) not in visited:
					q.append((ni,nj,bn,d+1))
	print(-1)

if __name__ == '__main__':
	main()
0