Forced Turn Grid Cost Problem

Forced Turn Grid Cost Problem — ExecCode Hard DSA Practice

Solve the Forced Turn Grid Cost problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Every cell contains an arrow: 1 right, 2 left, 3 down, 4 up. Following the arrow is free. Changing direction costs 1 (you rewrite that cell's sign). Reach the bottom-right with minimum rewrites. Return the minimum number of sign changes. The destination cell's arrow is never used. You are given: grid — m×n integers in {1,2,3,4} Return non-negative integer cost. Start at (0,0). Destination is (m-1, n-1).

Examples

Input {'grid': [[1, 1, 3], [3, 2, 2], [1, 1, 4]]}; Output 0. Input {'grid': [[1, 2], [4, 3]]}; Output 1. Input {'grid': [[4]]}; Output 0

Constraints

You always move to an adjacent cell; you cannot stay Changing a sign only affects the move leaving that cell

Practice Forced Turn Grid Cost free on ExecCode. Browse DSA problems, topic map, and placement guides.