Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 B
Each cell of a grid with 3 rows and 5 columns has an arrow: R = right, L = left, U = up, D = down. RURUR LRDLU RDDRD 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. 3
- B. 1
- C. 5
- D. 2
Correct answer: D. 2
A 0-1 BFS (cost 0 along the arrow, 1 otherwise) gives 2. Ignoring the arrows and counting moves gives 6, and the number of cells whose arrow is not R or D 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
- On an 8×8 chessboard, rows and columns are numbered 0 to 7, and squares are written (row, column). A knight moves in the usual L-shape…
- Bellman–Ford is run from source X on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: F→I…
- A tree has the vertices C, E, G, K, N, S, T, V, Z and the edges T–K, K–E, K–V, K–S, S–N, N–C, C–G, G–Z. A centroid is a vertex whose…
- A grid has 3 rows and 3 columns with these values (top to bottom): 787 591 837 A path moves between side-adjacent cells (up, down, left or…
- Consider this function: python def g(n): if n < 3: return n if n % 2 == 0: return g(n - 3) + g(n - 2) return g(n - 1) - 1 What value does…
- Consider this recursive function: python def f(n): if n <= 1: return f(n - 1) if n % 2 == 0: f(n - 2) How many times is f called in total…