Minimum Energy Path with Conveyor Belts and Magnetic Polarity
You are given a 2D grid of size $R \times C$ representing a laboratory floor. You control an automated vehicle starting at cell S and must reach target cell T while minimizing total energy consumption.
Each cell in the grid is represented by one of the following characters:
S: Starting cell. Functions as a normal floor cell. The vehicle begins here with a positive (+) magnetic charge.T: Target cell. Functions as a normal floor cell..: Normal floor cell. Entering costs $1$ energy unit. Vehicle charge is unchanged.#: Solid obstacle. Impassable.+: Positive magnetic tile. Entering costs $1$ energy unit if current charge is+, or $3$ energy units if current charge is-. Sets vehicle charge to+.-: Negative magnetic tile. Entering costs $1$ energy unit if current charge is-, or $3$ energy units if current charge is+. Sets vehicle charge to-.U,D,L,R: Directional conveyor tiles (Up, Down, Left, Right).
Conveyor Movement Rules:
- Stepping into a conveyor tile from an adjacent cell costs $1$ energy unit.
- Once on a conveyor tile, the vehicle is continuously forced in that tile's direction (
U= up,D= down,L= left,R= right). - Traversing each subsequent conveyor tile during a slide costs $1$ energy unit.
- Conveyor movement continues automatically until the vehicle lands on a non-conveyor cell (
.,+,-,S, orT). - Landing on the final non-conveyor cell adds its standard entrance cost (based on cell type and vehicle charge) to the total move energy, and updates vehicle charge if applicable.
- Invalid Moves: If a conveyor slide pushes the vehicle off the grid boundary, into an obstacle
#, or into an infinite loop of conveyor tiles, the initial move into that conveyor system is invalid and cannot be taken.
Return the minimum total energy required to reach T from S. If T cannot be reached, return -1.
[ "[\"S.+\", \"URD\", \"..T\"]" ]
Explanation. From S at (0,0) with initial charge '+', moving Right to (0,1) costs 1 energy unit. Moving Down into conveyor tile 'R' at (1,1) forces sliding through 'R' and 'D' to land on 'T' at (2,2). The conveyor slide and landing cost is 3. Total minimum energy cost is 4.
[ "[\"S - -\", \"# # -\", \"T . .\"]" ]
Explanation. Starting at (0,0) with initial charge '+', moving Right to (0,1) '-' costs 3 energy units and changes vehicle charge to '-'. Navigating around the obstacles via negative tiles to (1,2) and then through normal tiles to T at (2,0) yields a minimum total energy cost of 8.
[ "[\"S R L T\"]" ]
Explanation. Moving Right from S enters an infinite loop between 'R' at (0,1) and 'L' at (0,2). Because this loop never terminates, entering the conveyor system is invalid and target T is unreachable, returning -1.
[ "[\"S R R T\"]" ]
Explanation. Moving Right into 'R' at (0,1) slides across two conveyor tiles and lands on T at (0,3) for a total energy cost of 3.
[ "[\"S+\", \"-T\"]" ]
Explanation. From S at (0,0) with positive charge, moving Right to '+' at (0,1) costs 1 unit and retains positive charge. Moving Down to T at (1,1) costs 1 unit, giving a total cost of 2.
[ "[\"S # T\", \". # .\"]" ]
Explanation. Obstacles completely block all possible paths from S to T, so T is unreachable.
Follow-up: How would you handle a variant where the vehicle has a maximum energy battery capacity B, and certain cells act as recharging stations?
1 <= R, C <= 100 The grid contains exactly one 'S' and exactly one 'T'. Grid elements belong to {'S', 'T', '.', '#', '+', '-', 'U', 'D', 'L', 'R'}.