Data Structures & AlgorithmsVery hardICPC regional · Product Company SDE

Problem: On a grid, a diagonal step costs 2 and a straight step costs 1 (moves are up, down, left, right, and the four diagonals). What is the minimum cost to go from (0,0) to (3,5)?

  1. A. 8
  2. B. 5
  3. C. 10
  4. D. 6

Correct answer: A. 8

A diagonal step (cost 2) is never cheaper than two straight steps, so the cost is the Manhattan distance 3 + 5 = 8 (equivalently 3 diagonals plus 2 straight steps). Pricing diagonals at 1 gives the Chebyshev distance 5, pricing every one of max(3, 5) steps at 2 gives 10, and counting only the 3 diagonal steps gives 6.

More Coding Interview Problems questions

Exams that ask this topic