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?
- A. 6
- B. 8
- C. 2
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→D (1), S→B (6), C→B (9), D→A (9), D→T (8), B→D…
- A directed weighted graph has the arcs u→v (weight): Y→N (3), Y→E (9), Y→H (3), N→Y (3), N→E (4), N→H (7), E→N (15), E→A (11), H→Y (2)…
- An undirected graph has the vertices D, F, J, L, N, Y and the edges D–Y, Y–N, Y–J, N–F, F–J, J–L. In how many ways can each vertex be…
- A tree on the vertices F, I, P, S, T, W, Z has the arcs W→T, T→Z, W→S, W→F, P→T, P→I (each undirected edge is directed as shown). You may…
- Bellman–Ford is run from source J on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: C→J…
- Each cell of a grid with 3 rows and 3 columns has an arrow: R = right, L = left, U = up, D = down. ULU LUR ULU From a cell you may move to…