G |
Gathering
Food |
Winter is approaching!
The weather is getting colder and days are becoming shorter. The animals take different
measures to adjust themselves during this season.
-
Some of them "migrate." This means they travel to other places where
the weather is warmer.
-Few animals remain and
stay active in the winter.
-Some animals
"hibernate" for all of the winter. This is a very deep sleep. The
animal's body temperature drops, and its heartbeat and breathing slow down. In
the fall, these animals get ready for winter by eating extra food and storing
it as body fat.
For this problem, we are
interested in the 3rd example and we will be focusing on ‘Yogi
Bear’.
Yogi Bear is in the
middle of some forest. The forest can be modeled as a square grid of size N x N. Each cell of the grid consists
of one of the following.
. an empty space
# an obstacle
[A-Z] an English alphabet
There
will be at least 1 alphabet and all the letters in the grid will be distinct.
If there are n letters, then it will
be from the first n alphabets.
Suppose n = 3, that means there will be exactly 1 A, 1 B and 1 C.
The
letters actually represent foods lying on the ground. Yogi starts from position
‘A’ and sets off with a basket in the hope of collecting all other foods. Yogi
can move to a cell if it shares an edge with the current one. For some
superstitious reason, Yogi decides to collect all the foods in order. That is,
he first collects A, then B, then C and so on until he reaches
the food with the highest alphabet value. Another philosophy he follows is that
if he lands on a particular food he must collect it.
Help
Yogi to collect all the foods in minimum number of moves so that he can have a
long sleep in the winter. You are also required to find the total number of
shortest paths he can take that meets the above constraints.
Input
There
will be several cases in the input file. Each case starts with, N(N<=10), the size of the grid. Each
of the next N lines contains N characters each. Input ends with a
value of 0 for N.
Output
For
each case, output the case number first. If it’s impossible to collect all the
foods, output ‘Impossible’. Otherwise, print the shortest distance followed by
the total number of distinct paths. Since the total number of shortest paths
can be very big, output the result modulo 20437.
5 |
Case
|
Problem Setter : Sohel
Hafiz
Special Thanks :