haechandev
Algorithms

[Baekjoon 2169] Robot Control

Scan each row in both directions and combine the best values from the preceding row.

January 15, 2023

The robot can move down, left, and right, but never up. Allowing both horizontal directions makes a single directional DP insufficient: it can accidentally revisit a cell.

I computed two separate arrays for every row.

leftToRight[x] = max(previousRow[x], leftToRight[x - 1]) + value
rightToLeft[x] = max(previousRow[x], rightToLeft[x + 1]) + value
dp[x] = max(leftToRight[x], rightToLeft[x])

Scanning in both directions preserves the best value for entering a row from either side. I kept only one dp array for the preceding row to reduce memory usage.

Problem: Baekjoon · Code: View on GitHub