Earth has succumbed to a zombie outbreak and the survivors are struggling to stay alive. Rick, the leader of the survivors, has entered a maze while trying to escape a horde of starving zombies. To avoid being eaten he has to reach the exit of the maze as fast as possible. Also, if possible, he has to try to avoid a special chamber that is full of poisonous gas. The poison will not only make him slower, but also easier to detect by zombies. The objective is to help Rick and determine the fastest path between the entry and the exit points of the maze that, if possible, avoids the poisonous chamber. As a last resource our hero might have to run through the poisonous chamber, which will make him to be twice as slower as his usual speed.
A maze is represented by a rectangular grid composed of square chambers. Each movement from one chamber to an adjoining chamber (at north, south, east, or west) takes one time unit, if Rick has not passed through the poisonous chamber. If Rick passes through the poisonous chamber, he will become twice as slower as his usual speed.
The objective is to help Rick and determine the duration of the safest paths from the entry to the exit point of the maze. A path that avoids the poisonous chamber is safer than a path that passes through the poisonous chamber. Besides, faster paths are safer.
The first line contains two integers separated by a single space:
The following L lines contain the representation of the maze. Each line contains C characters. A dot “.” represents a chamber, a hash mark “#” represents a wall, “S” the start point, “E” the exit point, and “P” the poisonous chamber.
• | 2 ≤ L ≤ 400 | Number of lines | |
• | 2 ≤ C ≤ 400 | Number of columns |
The output consists of one line with information of the safest paths from the initial to the final chamber. The line has “YES”, if the safest paths avoid the poisonous chamber, and “NO” otherwise, followed by a space and the duration of the safest paths. When it is not possible to reach the maze exit, the output line contains “IMPOSSIBLE” .
6 7
S#...#.
.#.#.#.
.#.#.#.
.#.#.#.
.#.#.#.
...P..E
YES 21
6 7
S#...#.
.#.#.#.
.#.#.#.
.#.#.#.
.#.#.#.
.....PE
NO 12
6 7
S#...#.
.#.#.#.
.#.#.#.
.#.#.#.
.#.#.#.
...P.#E
IMPOSSIBLE