Transformation move game
Time limit2sMemory limit512 MB
Find the fewest turns to reach a target cell on an N by N board where you can walk one cell per turn in normal mode or, after paying t turns, warp to the nearest warp cell in a direction.
- Level
Medium6 of 10
- Topics
- Graph, BFS, Shortest path
- Solved
- No attempts yet
Problem
A character moves on an board from the start cell to the target cell. The character starts on in normal mode. The target cell is .
The character is either in normal mode or in transformation mode. In normal mode, one turn moves the character by one cell up, down, left, or right. In transformation mode, one turn moves the character to the nearest warp cell in one of the four directions from the current cell. When the chosen direction holds no warp cell, the character cannot move that way. In transformation mode only warp moves are allowed.
Changing from normal mode to transformation mode takes turns, and changing back takes no turns. Compute the minimum number of turns needed to reach the target cell from the start cell.
Input
The first line holds the board size , the transformation cost , and the target row and column , separated by spaces.
The next lines describe warp cells. Each line has characters, and each character is # or .. # marks a cell with a warp point and . marks a plain cell.
Output
Print the minimum number of turns needed to reach the target cell in one line. The target stays reachable on foot alone, so the answer always exists.
Hint
Walking in normal mode alone reaches the target. Transformation pays off when warp cells line up, and returning is free, so compare the arrival times of both modes on the same cell. A warp move from a warp cell still targets the nearest warp cell in the chosen direction, not the current cell.