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?

  1. A. 3
  2. B. 1
  3. C. 5
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic