結果

問題 No.424 立体迷路
ユーザー suppy193suppy193
提出日時 2016-10-26 14:39:48
言語 Ruby
(3.3.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,914 bytes
コンパイル時間 43 ms
コンパイル使用メモリ 11,584 KB
実行使用メモリ 15,348 KB
最終ジャッジ日時 2023-09-18 16:57:31
合計ジャッジ時間 3,458 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,300 KB
testcase_01 AC 82 ms
15,136 KB
testcase_02 AC 84 ms
15,036 KB
testcase_03 AC 84 ms
15,236 KB
testcase_04 AC 83 ms
15,108 KB
testcase_05 AC 85 ms
15,144 KB
testcase_06 AC 83 ms
15,324 KB
testcase_07 AC 83 ms
15,036 KB
testcase_08 AC 84 ms
15,148 KB
testcase_09 AC 82 ms
15,180 KB
testcase_10 AC 85 ms
15,236 KB
testcase_11 AC 82 ms
15,164 KB
testcase_12 AC 82 ms
15,144 KB
testcase_13 AC 82 ms
15,212 KB
testcase_14 AC 82 ms
15,220 KB
testcase_15 AC 82 ms
15,144 KB
testcase_16 AC 82 ms
15,108 KB
testcase_17 AC 84 ms
15,192 KB
testcase_18 AC 83 ms
15,324 KB
testcase_19 AC 83 ms
15,264 KB
testcase_20 AC 85 ms
15,204 KB
testcase_21 AC 81 ms
15,180 KB
testcase_22 AC 88 ms
15,168 KB
testcase_23 AC 82 ms
15,164 KB
testcase_24 AC 86 ms
15,224 KB
testcase_25 AC 85 ms
15,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:81: warning: assigned but unused variable - next_x
Main.rb:82: warning: assigned but unused variable - next_y
Syntax OK

ソースコード

diff #

hw = gets.split(" ").map(&:to_i)
@h = hw[0]
@w = hw[1]
sg = gets.split(" ").map(&:to_i)
@s = sg[0] - 1 + (sg[1] - 1) * @h
#p @s
sx = sg[0]
sy = sg[1]
@s = [sx, sy]

@g = sg[2] - 1 + (sg[3] - 1) * @h
#p @g
gx = sg[2]
gy = sg[3]
@g = [gx, gy]

@maze = []
@h.times do
	@maze << gets.strip.split('').map(&:to_i)
	#p @maze
end
@maze.flatten!
#p @maze


def dfs(pos, path)
	x = pos[0]
	y = pos[1]
	#p path
	[[1, 0], [0, -1], [-1,0], [0, 1]].each do |dir|
		dx = dir[0]
		dy = dir[1]
		next_x = x + dx
		next_y = y + dy
		#print "#{x},#{y} -> #{next_x}, #{next_y}\n"
		#next_pos = pos + dx + dy * @w
		#return if next_pos == @g
		#next if next_pos < 0 || next_pos >= @h * @w
		next if next_x < 1 || next_x > @h || next_y < 1 || next_y > @w
		next_pos = [next_x, next_y]
		next if path.include?(next_pos)
#		next if @maze[next_x - 1 + next_y * @w] 
		#p @maze[(next_x - 1) * @w + next_y - 1]
		next if (@maze[(next_x - 1) * @w + next_y - 1] - @maze[(x - 1) * @w + y - 1]).abs > 1
		if @s == [next_x, next_y]
			puts "YES"
			exit	
		end
		#p dir
		#p next_pos
		path << next_pos
		dfs(next_pos, path.clone)
	end
	
	[[1, 0], [0, -1], [-1,0], [0, 1]].each do |dir|
		dx = dir[0]
		dy = dir[1]
		next_x = x + 2 * dx
		next_y = y + 2 * dy
		#print "#{x},#{y} -> #{next_x}, #{next_y}\n"
		next if next_x < 1 || next_x > @h || next_y < 1 || next_y > @w
		next_pos = [next_x, next_y]
		next if path.include?(next_pos)
		next if @maze[(x + dx - 1) * @w + (y + dy) - 1] >= @maze[(x - 1) * @w + y - 1]
		next if @maze[(next_x - 1) * @w + next_y - 1] != @maze[(x - 1) * @w + y - 1]
		if @s == [next_x, next_y]
			puts "YES"
			exit	
		end
		#p dir
		#p next_pos
		#print "Bridge #{x}, #{y} -> #{next_x}, #{next_y}\n"
		path << next_pos
		dfs(next_pos, path.clone)
		
	end
	

end

next_x = 2
next_y = 5
#p @maze[(next_x - 1) * @w + next_y - 1]
#exit

if @s == @g
	puts "YES"
	exit
end

path = [@g]
dfs(path[0], path.clone)
puts "NO"

0