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 1107. These codes are dead ends and may never be shown at any moment: 0128, 0313, 0518, 0576, 1137, 1151, 1458, 1622, 1790, 1878, 2278, 2300, 2693, 3072, 3344, 3347, 3555, 4283, 4315, 4424, 4545, 4823, 5056, 5124, 5344, 5804, 5979, 6122, 6196, 6207, 6549, 6717, 7011, 7063, 7600, 7731, 7927, 8681, 8792, 8997, 9466, 9514, 9727, 9931, 9964. What is the minimum number of moves?
- A. 7
- B. 4
- C. 6
- D. 5
Correct answer: D. 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.
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 vertices D, I, K, N, O, R, T, Y and weighted edges (weights in brackets): Y–T (4), Y–I (2), Y–O (8), Y–K (3), O–N (5), O–R (7)…
- An undirected graph has the vertices C, O, Q, W, X, Z and the edges X–Q, Q–C, Q–O, W–C, X–C, O–Z, Q–Z, C–O, X–Z. What is the minimum…
- An undirected graph has the vertices C, G, T, W, X and the edges C–T, C–G, W–T, W–G, W–X, T–G, G–X. A walk may repeat vertices and edges…
- A grid has 6 rows and 6 columns (top to bottom; '.' open, '#' blocked): ..#... .....# .##... ...... ..#..# ...... You may only move right…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 20. Two sequences of steps…
- A grid has 4 rows and 4 columns (top to bottom; '.' open, '#' blocked): ..## .... .... .... You may only move right or down, and you…