Problem
A map is given, consisting of small squares. Each square can be either empty (denoted by a blank space) or contain an obstacle (denoted by the letter ""). Tasos is located in an empty square (denoted by the letter "") and Golfo is located in another (denoted by the letter "").
Tasos wants to meet Golfo. Each time, he can move to one of the neighboring squares (up, down, left, and right). Moving left or right costs unit, moving up costs units, while moving down costs nothing.
Our goal is to find the minimum cost for Tasos to reach Golfo.
Input
The first line of the input will contain a natural number : the dimension of the map. The next lines will contain the map. Each line will contain characters, each of which will either be a blank space, or one of the letters "", "", or "". There will be exactly one "" and one "". Also, consider that the characters on the borders of the map will always be "".
Output
The output should consist of one line containing either a natural number (the minimum cost of Tasos' movement from his initial position to where Golfo is located) or the word "" (in uppercase letters), if Tasos cannot reach Golfo.
Constraints
Execution time limit: 1 sec.
Memory limit: 64 MB.
Examples of Input - Output
1st
STDIN (golfo.in)
10
XXXXXXXXXX
X X
X T X
XXXXXXXX X
X X X
X X X
X XXXXXXX
X GX
X X
XXXXXXXXXX
STDOUT (golfo.out)
20
2nd
STDIN (golfo.in)
8
XXXXXXXX
X T X
XXXXXX X
X X X
X XXXXX
X X
X GX
XXXXXXXX
STDOUT (golfo.out)
IMPOSSIBLE
Comments