Woburn Challenge 2016-17 Round 3 - Senior Division

Among other things, the Pokémon series of video games is known for having interesting 2D puzzles for the player to solve. The latest installment, Pokémon Navy Green, is no exception!
As one of the game's level designers, you've been given the
responsibility of turning
The
Since your own objective is to make the players' lives miserable, you
want to design each room in such a way that the shortest possible path
from the starting cell to the destination cell is as long as possible.
The distance covered by a path between two cells is the number of
up/down/left/right moves that it involves. For each room, you'll need to
determine the maximum possible length of such a shortest path, and come
up with a room design (an arrangement of walls and starting/destination
cells) which yields that optimal distance between its starting and
destination cells. A room can be described as S
, the single
destination cell with an E
, each wall with a #
, and each
remaining empty cell with a .
. If there exist multiple optimal room
designs, any of them will do.
In test cases worth
In test cases worth another
Input Specification
The first line of input consists of a single integer
Output Specification
For each room, output five lines. The first of these lines should
consist of a single integer – the largest possible shortest distance
between the starting and destination cells.
The last four of these lines shall describe any valid room design which
yields that optimal distance.
Sample Input
2
2
3
Sample Output
5
S.
#.
..
E#
8
...
.#E
..#
..S
Sample Explanation
Note that, for both rooms, there exist other valid room designs (besides
the ones shown here) which would also yield the same optimal distances
of
Comments