結果

問題 No.367 ナイトの転身
ユーザー urutomurutom
提出日時 2016-04-29 22:52:18
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 724 bytes
コンパイル時間 34 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 40,108 KB
最終ジャッジ日時 2024-04-15 05:43:47
合計ジャッジ時間 4,425 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
19,356 KB
testcase_01 AC 78 ms
12,160 KB
testcase_02 AC 84 ms
12,160 KB
testcase_03 AC 77 ms
12,416 KB
testcase_04 AC 78 ms
12,288 KB
testcase_05 AC 75 ms
12,288 KB
testcase_06 AC 82 ms
12,288 KB
testcase_07 AC 79 ms
12,288 KB
testcase_08 AC 80 ms
12,160 KB
testcase_09 AC 78 ms
12,160 KB
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

DIRS=[
	[[1,2],[2,1],[-1,2],[2,-1],[1,-2],[-2,1],[-1,-2],[-2,-1]],
	[[1,1],[-1,1],[1,-1],[-1,-1]]
]
h,w=gets.split.map(&:to_i)
rh=0...h
rw=0...w
board=h.times.map{gets.chomp}
sx=sy=tx=ty=0
h.times{|i|w.times{|j|
	if board[i][j]==?S
		sx=i
		sy=j
	elsif board[i][j]==?G
		tx=i
		ty=j
	end
}}

q=[]
s=[sx,sy,0]
q.push s
visited={}
visited[s]=0
t1=[tx,ty,0]
t2=[tx,ty,1]
until q.empty?||visited[t1]||visited[t2]
	state=q.shift
	x,y,piece=state
	step=visited[state]
	DIRS[piece].each{|dx,dy|
		nx=x+dx
		ny=y+dy
		next unless rh===nx&&rw===ny
		np=board[nx][ny]==?R ? 1-piece : piece
		next_state=[nx,ny,np]
		next if visited[next_state]
		visited[next_state]=step+1
		q.push next_state
	}
end

puts visited[t1]||visited[t2]||-1
0