Data Structures & AlgorithmsHardGATE CS · ICPC regional

A four-wheel combination lock shows four digits 0–9 on wheels that wrap around (9 goes to 0 and 0 goes to 9). One move turns one wheel by one step up or down. The lock starts at 0000 and must reach the code 9309. These codes are dead ends and may never be shown at any moment: 0990, 1186, 2061, 2360, 2638, 2691, 3309, 3456, 3769, 3800, 3840, 4144, 4278, 4428, 4479, 4690, 4815, 4817, 5964, 6089, 6184, 6672, 6721, 6773, 7038, 7865, 7964, 8103, 8484, 8830, 8997, 9046, 9109, 9292, 9936, 9987. What is the minimum number of moves?

  1. A. 5
  2. B. 7
  3. C. 6
  4. D. 4

Correct answer: A. 5

A BFS over the 10 000 codes that skips the dead ends gives 5 moves. Ignoring the dead ends gives 5, and summing the circular distance of each wheel gives 5.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic