Java Reference
In-Depth Information
The object of the game is to move from the first column to the last column in the
lowest total cost. The number in each column represents the cost to enter that
column. You always start the game in the first column and have two types of
moves. You can either move to the adjacent column or jump over the adjacent
column to land two columns over. The cost of a game is the sum of the costs of
the visited columns.
In the board shown above, there are several ways to get to the end. Starting in the
first column, our cost so far is 0. We could jump to 80, then jump to 57, then
move to 10 for a total cost of 80+57+10 = 147. However, a cheaper path would
be to move to 3, jump to 6, then jump to 10, for a total cost of 3+6+10 = 19.
Write a recursive solution to this problem that computes the cheapest cost of the
game and outputs this value for an arbitrarily large game board represented as an
array. Your program doesn't have to output the actual sequence of jumps, only
the cheapest cost of this sequence. After making sure that your solution works on
small arrays, test your solution on boards of larger and larger values of n to get a
feel for how efficient and scalable your solution is.
5.
Write a recursive method definition for a static method that has one parameter n
of type int and that returns the n th Fibonacci number. The Fibonacci numbers
are F 0 is 1, F 1 is 1, F 2 is 2, F 3 is 3, F 4 is 5, and in general
F i+2 = F i + F i+1 for i = 0, 1, 2, ...
Place the method in a class that has a main that tests the method.
6.
The formula for computing the number of ways of choosing r different things
from a set of n things is the following:
C ( n , r ) = n! /( r! *( n
āˆ’
r ) ! )
The factorial method n! is defined by
n! = n *( nāˆ’
1)*( nāˆ’
2)*...*1.
Discover a recursive version of the formula for C ( n , r ) and write a recursive
method that computes the value of the formula. Place the method in a class that
has a main that tests the method.
7.
Towers of Hanoi. There is a story about Buddhist monks who are playing this
puzzle with 64 stone disks. The story claims that when the monks finish moving
the disks from one post to a second via the third post, time will end.
A stack of n disks of decreasing size (from bottom to top) is placed on one of three
posts. The task is to move the disks one at a time from the first post to the second.
To do this, any disk can be moved from any post to any other post, subject to the
rule that you can never place a larger disk over a smaller disk. The (spare) third post
is provided to make the solution possible. Your task is to write a recursive static
method that gives instructions for a solution to this problem. We don't want to
Search WWH ::




Custom Search