PDP-25 (2013) - Camp junior - 2 (golfo)

View as PDF

Submit solution

Points: 30
Time limit: 1.0s
Memory limit: 64M

Author:
Problem types
Allowed languages
C, C++, Java, Pascal, Python

Problem

A map is given, consisting of N \times N small squares. Each square can be either empty (denoted by a blank space) or contain an obstacle (denoted by the letter "X"). Tasos is located in an empty square (denoted by the letter "T") and Golfo is located in another (denoted by the letter "G").

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 1 unit, moving up costs 2 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 N: the dimension of the map. The next N lines will contain the map. Each line will contain N characters, each of which will either be a blank space, or one of the letters "X", "T", or "G". There will be exactly one "T" and one "G". Also, consider that the characters on the borders of the map will always be "X".

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 "IMPOSSIBLE" (in uppercase letters), if Tasos cannot reach Golfo.

Constraints

4 \le N \le 500
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

There are no comments at the moment.