Skip to content

Instantly share code, notes, and snippets.

@whatalnk whatalnk/abc089-d.rb
Created Oct 16, 2018

Embed
What would you like to do?
AtCoder ABC #089 D. Practical Skill Test
h, w, d = gets.chomp.split(" ").map(&:to_i)
g = Array.new(h*w+1)
h.times do |i|
row = gets.chomp.split(" ").map(&:to_i)
w.times do |j|
a = row[j]
g[a] = [i, j]
end
end
a = Array.new(h*w+1, 0)
1.upto(h*w-d) do |i|
x0, y0 = g[i]
x1, y1 = g[i+d]
cost = (x1 - x0).abs + (y1 - y0).abs
a[i+d] = a[i] + cost
end
q = gets.chomp.to_i
q.times do
l, r = gets.chomp.split(" ").map(&:to_i)
if l == r
puts 0
else
puts a[r] - a[l]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.