Java Reference
In-Depth Information
If it's not possible to go in any direction, “back up” to a previous location in the maze and try a new
direction for that location (this is an example of recursive backtracking). Program the method to
display the maze after each move so the user can watch as the maze is solved. The final output of
the maze should display only the path needed to solve the maze—if going in a particular direction
results in a dead end, the
x
's going in that direction should not be displayed. [
Hint:
To display only
the final path, it may be helpful to mark off spots that result in a dead end with another character
(such as
'0'
).]
18.21
(Generating Mazes Randomly)
Write a method
mazeGenerator
that takes as an argument a
two-dimensional 12-by-12 character array and randomly produces a maze. The method should also
provide the starting and ending locations of the maze. Test your method
mazeTraversal
from
Exercise 18.20, using several randomly generated mazes.
18.22
(Mazes of Any Size)
Generalize methods
mazeTraversal
and
mazeGenerator
of
Exercise 18.20 and Exercise 18.21 to process mazes of any width and height.
18.23
(Time to Calculate Fibonacci Numbers)
Enhance the Fibonacci program of Fig. 18.5 so
that it calculates the approximate amount of time required to perform the calculation and the num-
ber of calls made to the recursive method. For this purpose, call
static
System
method
current-
TimeMillis
, which takes no arguments and returns the computer's current time in milliseconds.
Call this method twice—once before and once after the call to
fibonacci
. Save each value and cal-
culate the difference in the times to determine how many milliseconds were required to perform the
calculation. Then, add a variable to the
FibonacciCalculator
class, and use this variable to deter-
mine the number of calls made to method
fibonacci
. Display your results.