The Desert. Failing water, dying camels, howling wind. The desert wind
has hands -- it throws sand in your face, pushes you down, tries to bury you.
We are stranded in the desert.
But we have a map showing where we are, where all the impassable
terrain is located, and where every oasis is located. The map is divided into
square cells, and our trip will be a sequence
of legs, where each leg goes from one cell to one of the 8 adjacent cells. We
must start at our current location and make it to an oasis, any oasis.
Of course, we cannot use any
impassable cell and cannot leave the mapped area. Our survival depends on
how many days it will take to make the journey.
That evil Desert Wind is our nemesis! Each leg takes one day unless we are
traveling directly against the wind. In that case the leg will take three
days. Fortunately, the wind's direction can be determined just before setting
out on a leg and will not change for at least a day.
Create a class DesertWind that contains the method daysNeeded that
takes a String[] theMap and returns the number of days required, assuming the worst
possible wind conditions will occur. Return -1 if no path to an oasis exists.
Each element of theMap gives the terrain for the cells (in west to east order) at
a particular latitude; the first element is the most northern, the last element
is the most southern on the map. Each cell is indicated by a single character:
- '*' is an oasis
- 'X' is impassable
- '@' is the starting location
- '-' is sand
|