Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B
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 4389. These codes are dead ends and may never be shown at any moment: 0004, 0153, 0532, 0668, 0964, 1124, 1152, 1194, 1257, 1602, 2015, 2182, 2321, 2832, 2973, 2991, 3013, 3084, 3426, 3587, 4214, 4633, 5020, 5318, 5366, 5401, 5692, 5972, 6360, 6702, 7197, 7282, 7307, 7898, 7998, 8140, 8160, 8219, 8238, 8880, 9089, 9494, 9731. What is the minimum number of moves?
- A. 24
- B. 4
- C. 10
- D. 12
Correct answer: C. 10
BFS over codes that skips the dead ends finds 10 moves; here no dead end lies on a shortest route, so it equals the sum of circular wheel distances (4 + 3 + 2 + 1). 24 turns each wheel without wrapping, 4 is only the largest single-wheel distance, and 12 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
- An undirected connected graph on the vertices B, M, Q, R, U has the edges B–U, M–R, Q–R, M–Q, B–R, B–M, Q–U, B–Q. How many spanning trees…
- Each cell of a grid with 3 rows and 3 columns has an arrow: R = right, L = left, U = up, D = down. LUL LRD RUU From a cell you may move to…
- A directed graph has the vertices B, D, E, M, Y and the arcs B→M, B→E, B→D, B→Y, M→B, M→E, M→D, E→B, E→Y, D→B, D→M, D→E, D→Y, Y→M. Count…
- An undirected graph has the vertices C, P, T, X and the edges T–P, C–P, C–X, P–X. In how many ways can each vertex be given one of 3…
- Cities B, C, F, T, U are joined by roads with these travel costs: B–U 20, B–T 13, B–C 7, B–F 17, U–T 4, U–C 9, U–F 2, T–C 11, T–F 6, C–F…
- A tree has vertices with weights in brackets: E[6], C[8], O[8], B[11], X[14], D[9], T[5], M[1]. The edges are E–C, C–O, E–B, B–X, X–D…