Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created June 1, 2017 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/aceb65aa22d306f2c853c0198022c63d to your computer and use it in GitHub Desktop.
Save whatalnk/aceb65aa22d306f2c853c0198022c63d to your computer and use it in GitHub Desktop.
Codeforces #417 Div2
d = [1,2,3,4]
a_p_ = Array.new(5, false)
a_from = Array.new(5, false)
a_to = Array.new(5, false)
4.times do |i|
l, s, r, p_ = gets.chomp.split(" ").map(&:to_i)
if l == 1
a_from[i+1] = true
l_to = d[i - 1]
a_to[l_to] = true
end
if s == 1
a_from[i+1] = true
s_to = d[i - 2]
a_to[s_to] = true
end
if r == 1
a_from[i+1] = true
r_to = d[i - 3]
a_to[r_to] = true
end
if p_ == 1
a_p_[i+1] = true
end
end
5.times do |i|
if a_p_[i]
if a_from[i] || a_to[i]
puts "YES"
exit
end
end
end
puts "NO"
N, M = gets.chomp.split(" ").map(&:to_i)
$bldg = Array.new()
nflr = 0
N.times do
flr = gets.chomp.split("").map(&:to_i)
$bldg << flr
nflr += 1 if flr.include?(1)
end
$bldg.reverse!
$ret = []
def hausmeister(i, s, t, last)
curr = 0
flr = $bldg[i]
if flr.include?(1) then
if s == 0 then
pos = flr.rindex(1)
curr += pos
else
pos = flr.index(1)
curr += (M + 2 - pos - 1)
end
left = pos + 1
right = M + 2 - pos
if i == N - 1 then
$ret << t + curr
return t + curr
end
hausmeister(i+1, 0, t + curr + left, left)
hausmeister(i+1, -1, t + curr + right, right)
else
if i == N - 1 then
$ret << t - last
return t - last
end
if s == 0 then
pos = 0
else
pos = M + 1
end
left = pos + 1
right = M + 2 - pos
hausmeister(i+1, 0, t + left, last + left)
hausmeister(i+1, -1, t + right, last + right)
end
end
if nflr > 0 then
hausmeister(0, 0, 0, 0)
puts $ret.min
else
puts 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment