Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
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 4571. These codes are dead ends and may never be shown at any moment: 0155, 0313, 0441, 0456, 0562, 0612, 1082, 1525, 1810, 1841, 1897, 2042, 2189, 2398, 2440, 2557, 2603, 2684, 2904, 3350, 3372, 3886, 4740, 4953, 5021, 5304, 5489, 6021, 6072, 6239, 6814, 6919, 7370, 7560, 7930, 8233, 8235, 8267, 8440, 8656, 8666, 8699, 8719, 8731, 8743, 8999, 9003, 9065, 9317, 9661. What is the minimum number of moves?
- A. 17
- B. 5
- C. 13
- D. 15
Correct answer: C. 13
BFS over codes that skips the dead ends finds 13 moves; here no dead end lies on a shortest route, so it equals the sum of circular wheel distances (4 + 5 + 3 + 1). 17 turns each wheel without wrapping, 5 is only the largest single-wheel distance, and 15 assumes a dead end forces a two-move detour.
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 tree has the vertices B, E, H, I, Q, R, U, V and the edges V–I, I–H, H–R, H–U, R–B, I–E, U–Q. A centroid is a vertex whose removal…
- A tree on the vertices B, C, D, H, J, T, Z has the arcs T→B, Z→T, B→C, B→D, J→D, J→H (each undirected edge is directed as shown). You may…
- A grid has 4 rows and 4 columns with these values (top to bottom): 9446 8699 5398 3165 A path moves between side-adjacent cells (up, down…
- Floyd–Warshall runs on vertices 1 to 5 with these arcs u→v (weight): 1→2 (4), 1→3 (2), 2→1 (11), 3→1 (8), 3→2 (4), 3→4 (7), 3→5 (4), 4→1…
- Bellman–Ford is run from source U on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: K→U…
- 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…