You are given an
grid. Some cells, marked with ‘#’, have a wall. The rest of
cells are free, and they are marked with ‘.’. There are two
exceptions: one free cell is marked with ‘S’ (it is your
starting position), and another free cell is marked with
‘T’ (it has a treasure).
Your goal is to reach the treasure as fast as possible. Every second, you can either move to an adjancent free cell, or hit an adjancent wall with a hammer. You know that every wall vanishes after hits.
Input consists of several cases, each with , and , followed by lines with characters each. Assume that and are between 1 and 1000, and that is between 1 and .
For every case, print the minimum time to reach the treasure from the starting position.
Input
1 2 20 ST 2 3 10 S.. ..T 2 3 10 S## ##T 3 3 10 T.. ##. S.. 3 3 3 T.. ##. S.. 4 6 100000 T##S#. ..###. ...#.. ......
Output
1 3 23 6 5 100013