Data Structures & AlgorithmsVery hardGATE 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 4020. These codes are dead ends and may never be shown at any moment: 0109, 0521, 0721, 0725, 0781, 0796, 1005, 1017, 1246, 1783, 1883, 2213, 2637, 2686, 2719, 2889, 3248, 3371, 3464, 3465, 3947, 4557, 4795, 5146, 5565, 5702, 6142, 6288, 6656, 6677, 6945, 7176, 7820, 7985, 8302, 8420, 8457, 8776, 8943, 9323, 9384, 9921. What is the minimum number of moves?

  1. A. 6
  2. B. 8
  3. C. 2
  4. D. 14

Correct answer: A. 6

BFS over codes that skips the dead ends finds 6 moves (4 on the first wheel, 2 on the third); no dead end blocks a shortest route. 8 assumes a two-move detour, 2 counts the wheels that move, and 14 turns those wheels the long way round.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic