Question

The game of “Jump It” consists of a board with n positive integers in a row,...

The game of “Jump It” consists of a board with n positive integers in a row, except for the first
column, which always contains zero. These numbers represent the cost to enter each column.
Here is a sample game board where n is 6:
0 3 80 6 57 10
The object of the game is to move from the first column to the last column with 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, and 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 function that solves this problem and returns the lowest cost of a game board
represented and passed as an array.
Note: your function shouldn’t output the actual sequence of jumps, only the lowest cost of this
sequence.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

#include<bits/stdc++.h>
using namespace std;
int recursiveCost(int *arr, int currentIndex, int numberOfElements)
{
    //base cases
    if(currentIndex==numberOfElements-1)    //we reached end of column
        return 0;
    if(currentIndex==numberOfElements-2)    //neighbor column is the last column
        return arr[currentIndex+1]; //cost to move to last column

    //compare neighbor element and adjacent to neighbor
    return min(arr[currentIndex+1]+recursiveCost(arr, currentIndex+1, numberOfElements),
               arr[currentIndex+2]+recursiveCost(arr, currentIndex+2, numberOfElements));
}
int main()
{
    int arr[] = {0, 3, 80, 6, 57, 10};
    int n = 6; //number of elements
    int currentIndex=0;
    cout<<"Lowest cost is "<<recursiveCost(arr, currentIndex, n);
}

Below is the screenshot of output

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any

Add a comment
Know the answer?
Add Answer to:
The game of “Jump It” consists of a board with n positive integers in a row,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use C++. The game of “Jump It” consists of a board with n positive integers in...

    Use C++. The game of “Jump It” consists of a board with n positive integers in a row, except for the first column, which always contains zero. These numbers represent the cost to enter each column. Here is a sample game board where n is 6: 0 3 80 6 57 10 The object of the game is to move from the first column to the last column with the lowest total cost. The number in each column represents the...

  • MATLAB simulate the game "Threes!" in MATLAB. The "game board" should take place on a 4x4...

    MATLAB simulate the game "Threes!" in MATLAB. The "game board" should take place on a 4x4 matrix. Simulate empty matrix cells with zeros. o a zero isan empaty space and a non-empty sG-) An example 0 2 2 1 Gameboard vw1 230 Thejndividual custom function should be able to take any ONE number within the gameboard id axci tme i funcon should uillixe branches io accout for all pssibl scenarios (the number is already against the wall, it is blocked...

  • Connect 4 is a 2 player game where each player has a set of colored tokens...

    Connect 4 is a 2 player game where each player has a set of colored tokens (red or yellow). Players take turns during which they place a single token into one of the columns of an n by m grid (where n is the number of rows and m is the number of columns. They place their token into a slot at the top of the column and it falls into the lowest unoccupied slot in that column. A player...

  • In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o i...

    In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member called gameState that holds one of the following values: X_WON, O_WON, or UNFINISHED - use an enum type for this, not string (the...

  • Question 3: A smaller version of Connect 4 is played on a vertical board 3-row high and 3-column ...

    Question 3: A smaller version of Connect 4 is played on a vertical board 3-row high and 3-column wide. The two players Maxim and Minnie alternately drop a black and white disc, respectively, from the top of any column, and it will fall to the lowest open row. A player wins when three of his/her discs are aligned either horizontally, vertically, or diagonally; otherwise the game is a draw. Consider the sample game below, where the first four moves have...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • Perpetual Inventory Using LIFO Beginning inventory, purchases, and sales data for portable game players are as...

    Perpetual Inventory Using LIFO Beginning inventory, purchases, and sales data for portable game players are as follows: Apr. 1 Inventory 77 units @ $76 10 Sale 56 units 15 Purchase 98 units @ $80 20 Sale 57 units 24 Sale 15 units 30 Purchase 27 units @ $84 The business maintains a perpetual inventory system, costing by the last-in, first-out method. Determine the cost of merchandise sold for each sale and the inventory balance after each sale, presenting the data...

  • Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLE...

    Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLEASE PROVIDE COMMENTS AND OUTPUT! Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member...

  • INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first....

    INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first. But rolling certain numbers will change the pace of the game. INCLUDE IN YOUR ASSIGNMENT: Annotation is a major part of any program. At the top of each of your C++ programs, you should have at least four lines of documentation: // Program name: tictactoe.cpp // Author: Twilight Sparkle // Date last updated: 5/26/2016 // Purpose: Play the game of Tic-Tac-Toe ASSIGNMENT: Sorry Game...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT