Racing
InterviewTime limit2sMemory limit1024 MB
A car slides from its cell toward a chosen wall, stops half the remaining distance from the first obstacle, and we need the fewest presses to reach the target.
- Level
Medium6 of 10
- Topics
- BFS, Graph, Simulation, Implementation
- Solved
- No attempts yet
Problem
For his birthday, young technician Misha received a radio-controlled car. Misha quickly grew bored of driving the car back and forth around the room, so he built a special track. He divided the room into square cells, left some of them empty, and placed obstacles in others. For a whole week, Misha improved his record for completing the track every day. But how disappointed he was when his friend Tima came to visit with his own car and beat his record. It became clear that the car had to be upgraded.
During test runs made a day later, Misha discovered that the car did indeed drive better, but its behavior had changed somewhat. Now only four buttons on the remote work: forward, backward, right, left. When one of them is pressed, the car drives toward the corresponding wall of the room, which is also the boundary of the track, exactly perpendicular to that wall. The car accelerates so much that it stops responding to other commands, crashes into the nearest obstacle or wall, and bounces off it by half the distance it traveled. That is, if there were empty cells between the car and the wall, then after the bounce it stops on the cell that is cells from the wall ( denotes rounding down; for example, , ).

Now Misha wonders what the minimum number of times the remote buttons must be pressed for the car, starting in the start cell, to stop in the finish cell.
Input
The first line of the input file contains two integers and , the dimensions of the track (). The next lines contain characters each: the character <<.>> corresponds to an empty cell, <<\#>> to an obstacle, and <<S>> and <<T>> to the start cell and the finish cell, respectively.
Output
Print the minimum number of button presses on the remote needed to drive the car along the track from the start to the finish.
If it is impossible to get from the start to the finish, print .