class Questions::Issue20260316
Question
Youβre given a 2D grid representing a city where each cell is either empty (0), a fire station (1), or a building (2). Fire stations can serve buildings based on horizontal + vertical moves only. Return a 2D grid where each cell shows the minimum distance to the nearest fire station.
Examples
Public Instance Methods
Source
# File questions/20260316.rb, line 20 def setup @methods = Answers::Issue20260316.instance_methods(false) @examples = [ { city_grid: [ [2, 0, 1], [0, 2, 0], [1, 0, 2], ], expected: [ [2, 1, 0], [1, 2, 1], [0, 1, 2], ], }, { city_grid: [ [1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 1], ], expected: [ [0, 1, 1, 0], [1, 2, 2, 1], [1, 2, 2, 1], [0, 1, 1, 0], ], }, ] end
Tests
Public Instance Methods
Source
# File questions/20260316.rb, line 54 def test_answers_with_examples @methods.each do |method| @examples.each do |example| actual = public_send(method, example[:city_grid].map(&:clone)) assert_equal example[:expected], actual, "Answer #{method} is not correct" refute_same example[:city_grid], actual, "Answer #{method} is messing with the outer city grid" refute_same example[:city_grid].first, actual.first, "Answer #{method} is messing with the inner city grid" end end end