Interview Questions/Coding/Cheapest Direction Repair

Cheapest Direction Repair

Preview mode. Log in to edit, run, submit, and save progress.

Hard

Cheapest Direction Repair

A robot is placed in the top-left room of a rectangular control grid. Each room contains a command: 1 sends the robot right, 2 sends it left, 3 sends it down, and 4 sends it up. You may rewrite any room command, and each rewrite costs 1. Return the minimum total cost needed so the robot can follow commands from the top-left room to the bottom-right room.

Examples

Example 1
Input:
2 2
1 3
1 3
Output:
0

Explanation: The existing commands already allow a path from the start to the target.

Approach hint

Moving in the cell's current direction costs 0; choosing any other direction costs 1.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp