Woburn Challenge 2018-19 Finals Round - Senior Division

Two particularly famous actors are starring in the cows' and monkeys' production: Tom Cows and Monkey Freeman. With both of them playing equally important roles, one question is at the forefront of everybody's mind: which of them will receive top billing in the credits and get to be the main face of the movie?
Tom isn't one to stand for playing second fiddle, so he's hatching a plan to discredit Monkey in the eyes of the director, Bo Vine. He's going to sneak into the main studio building in the middle of the night and rearrange its layout, in the hopes of causing Monkey to be late for the following morning's filming session. That'll show him!
The building can be represented as a grid with
In the morning, Tom and Monkey will begin in the starting cell and will
each attempt to make their way to the ending cell, by repeatedly moving
from cell to adjacent cell (up, right, down, or left) at a rate of
- Make an ordered list of possible next cells to move to as follows:
for each of the directions up/right/down/left (in that order), if
the cell adjacent to his current one in that direction is within the
grid and doesn't contain a wall, include it in the list. Note that
this list will always contain between
and cells, inclusive. - Move to the cell in the list which is closest to the ending cell by Manhattan distance* (ignoring any walls). If multiple cells in the list have the same minimum Manhattan distance to the ending cell, choose the one which appears earliest in the list.
- If he's arrived at the ending cell, stop. Otherwise, return to step
.
Note: The Manhattan distance between a cell in (row
For example, consider the following two building layouts (with S
denoting the starting cell, E
denoting the ending cell, and walls
marked with #
):
Copy
|
Monkey would reach the ending cell after |
Copy
|
Monkey would walk around forever (following the sequence of moves: down, down, down, up, down, up, down, up, ...). |
Help Tom come up with any building layout which would cause Monkey to
successfully arrive at the ending cell from the starting one eventually
(rather than walking around forever as in the second example above), but
at least
Subtasks
In test cases worth
Input Specification
The first and only line of input consists of two space-separated
integers,
Output Specification
Output
S
: starting cell (which must appear exactly once)E
: ending cell (which must appear exactly once).
: vacant cell#
: wall
Sample Input 1
4 2
Sample Output 1
ES
..
.#
#.
Sample Explanation 1
For this grid, both Tom and Monkey would reach the ending cell in
Sample Input 2
10 9
Sample Output 2
.........
.........
.........
.........
.........
.........
.........
....#....
.S#...#E.
.........
Sample Explanation 2
For this grid, Monkey would reach the ending cell in 10 minutes
(following the sequence of moves: up, right, right, down, right, right, up, right, right, down), while Tom would only
require
Comments