Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
Two unmarked jugs hold 7 litres and 9 litres. Allowed operations: fill a jug completely, empty a jug completely, or pour one jug into the other until the source is empty or the destination is full. Both jugs start empty. What is the minimum number of operations needed so that one of the jugs contains exactly 4 litres?
- A. 6
- B. 8
- C. 3
- D. 2
Correct answer: A. 6
BFS over (first jug, second jug) states gives 6 operations: fill the 9, pour into the 7, empty the 7, pour, fill the 9, pour, leaving 4 in the 9. Requiring the target in the 7-litre jug takes 8; 3 counts only the pours and 2 only the fills of the optimal route.
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 E, H, I, K, L, M, V, Z and weighted edges (weights in brackets): I–E (5), I–L (9), I–H (9), E–K (6), K–Z (7), E–M (1)…
- A directed acyclic graph has the vertices D, E, I, Q, R, U, Z and the edges D→R, D→E, Z→D, Z→R, Z→Q, Z→E, R→U, Q→D, I→D. A topological…
- A directed graph has the vertices H, J, M, T, X, Y and the arcs M→T, M→X, T→X, J→Y, X→Y, Y→T, Y→J. How many strongly connected components…
- A tree has the vertices D, E, I, L, O, P, Q, T, W and the edges T–D, D–L, L–Q, Q–E, T–I, E–O, T–P, D–W. A centroid is a vertex whose…
- The directed acyclic graph has the vertices E, H, J, K, S, T, V, W and the arcs W→E, S→K, J→H, V→H, J→K, V→W, W→K, E→H, J→T, T→E, V→S…
- Grid with 3 rows and 3 columns (top to bottom). A cell marked G is a gate; '.' is a normal cell: .GG G.. ... Each step, which is a move to…