Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional
Each cell of a grid with 3 rows and 5 columns has an arrow: R = right, L = left, U = up, D = down. RDLLR UUDUR LDUDD From a cell you may move to any of its four side-adjacent cells. Moving in the direction of the cell's arrow costs 0; moving in any other direction costs 1. What is the minimum total cost from the top-left cell to the bottom-right cell?
- A. 7
- B. 2
- C. 4
- D. 3
Correct answer: D. 3
A 0-1 BFS (cost 0 along the arrow, 1 otherwise) gives 3. Ignoring the arrows and counting moves gives 6, and the number of cells whose arrow is not R or D gives 7.
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 directed graph with no directed cycles has arcs u→v (weight) : U→A (-3), U→V (4), A→Z (3), V→O (2), Z→O (5). Weights may be negative…
- A directed acyclic graph has the vertices C, D, G, J, O, S, T and the directed edges D→J, G→C, G→O, S→O, C→J, D→O, O→J, D→T, C→O, S→C…
- An undirected graph has the vertices B, C, F, L, O, P, Q, R, V, Z and the edges Q–L, Q–Z, Q–V, B–O, B–L, B–Z, B–R, B–P, O–Z, O–F, O–V…
- A grid has 6 rows and 4 columns (top to bottom; '.' open, '#' blocked): .... #... .... #... ..#. ..#. You may only move right or down, and…
- A bipartite graph has left side P1, P2, P3 and right side Q1, Q2, Q3, Q4, Q5, with edges P1–Q5, P1–Q3, P3–Q4, P1–Q2, P2–Q1, P2–Q4, P3–Q3…
- A directed graph has the vertices E, G, Q, Z and the arcs E→Q, E→Z, G→E, G→Q, G→Z, Q→G, Q→Z, Z→E. Count the directed Hamiltonian paths…