University of Toronto ACM-ICPC Tryouts 2013
This evening, Alice and Bob have decided to simply go out on a nice walk
together. On the way, they'll traverse
different
sidewalks. Now that they won't be watching engrossing movies, Bob really
wouldn't mind holding Alice's hand - however, this can sometimes be
difficult to maintain, due to potential obstacles in their way.
A given sidewalk can be modelled as a two-dimensional grid of cells,
with
rows and
columns. The
cell in the
row is referred to as cell
. Each
cell is either walkable (represented by a
.
), or is blocked by the
neighbourhood dog's droppings (#
). Alice and Bob will be walking
Southwards along it, independently of one another, but both at a rate of
exactly one row per second. Needless to say, they'll never walk through
cells containing droppings, nor can they ever occupy the same cell as
one another. Each of them can start in any cell on row 1, and each
second, will move down by a cell, as well as optionally left or right by
a cell. In other words, if Alice or Bob is in cell , they can
then move to either cell
,
, or
,
as long as they don't step off the sidewalk. This continues until
they've both made it to row
, at which point they step off and make
their way towards the next sidewalk on their route. It might also be the
case that they're unable to both navigate to the end of the sidewalk, in
which case they'll take a detour around it entirely.
Along the way, whenever Alice and Bob find themselves side-by-side (in
other words, in cells and
, for some
and
),
they can hold hands for that second! Bob is interested in coordinating
their walk to enable them to hold hands for as long as possible.
Input Specification
Line 1: 1 integer,
For each sidewalk:
Line 1: 2 integers, and
Next lines:
characters, representing the
row of the
sidewalk grid, for
Output Specification
For each sidewalk, output 1 integer, the maximal number of seconds Alice
and Bob can spend holding hands, or the string Detour
if they can't
both make it to the end.
Sample Input
2
5 5
#....
..##.
.#...
#.#..
....#
2 4
.##.
..##
Sample Output
4
Detour
Explanation of Sample
On the first sidewalk, Bob can ask Alice to walk through cells ,
,
,
, and
, while he goes through cells
,
,
,
, and
. The two of them will then get to
hold hands while on rows
,
,
, and
, for a total of
seconds.
However, there's no way for both Alice and Bob to navigate the second
sidewalk. They must start in cells and
, and no walkable
cells on the second row can be reached from cell
.
Comments