Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3.  
  4. /*
  5.  
  6. Q2: The game of “Jump It” consists of a board with n positive integers
  7. in a row except for the first column, which always contains zero. These
  8. numbers represent the cost to enter each column. Here is a sample game
  9. board where n is 6:
  10.                    
  11. 0   3   80  6   57  10
  12.  
  13. The object of the game is to move from the first column to the last
  14. column in the lowest total cost. The number in each column represents
  15. the cost to enter that column. You always start the game in the first
  16. column and have two types of moves. You can either move to the adjacent
  17. column or jump over the adjacent column to land two columns over. The
  18. cost of a game is the sum of the costs of the visited columns.
  19.  
  20. In the board shown above, there are several ways to get to the end.
  21. Starting in the first column, our cost so far is 0. We could jump to
  22. 80, then jump to 57, then move to 10 for a total cost of 80+57+10 = 147.
  23. However, a cheaper path would be to move to 3, jump to 6, then jump to
  24. 10, for a total cost of 3+6+10 = 19.
  25.  
  26. Write a recursive solution to this problem that computes the cheapest
  27. cost of the game and outputs this value for an arbitrarily large game
  28. board represented as an array. Your program doesn’t have to output the
  29. actual sequence of jumps, only the cheapest cost of this sequence.
  30. After making sure that your solution works on small arrays, test your
  31. solution on boards of larger and larger values of n to get a feel for
  32. how efficient and scalable your solution is.
  33. */
  34.  
  35. int jumpIt(int game[5], int spot, int count)
  36. {
  37.     int jumpValue = 0;
  38.     int nextValue = 0;
  39.     int i = spot;
  40.     int jump = spot + 2;
  41.     int next = spot + 1;
  42.  
  43.     if (i==5) return count;
  44.  
  45.     else
  46.    
  47.     nextValue = game[next];
  48.     jumpValue = game[jump];
  49.  
  50.     if (jumpValue > nextValue)
  51.     {
  52.         count = nextValue + count;
  53.         count = jumpIt(game, next, count);
  54.     }
  55.    
  56.     else
  57.  
  58.     {
  59.         count = jumpValue + count;
  60.         count = jumpIt(game, jump, count);
  61.     }
  62.     return count;
  63. }
  64.  
  65. void main()
  66. {
  67.     int game[5];
  68.     int i;
  69.     int count;
  70.     count = 0;
  71.     i = 0;
  72.     int result;
  73.     game[0] = 0;
  74.     game[1] = 3;
  75.     game[2] = 80;
  76.     game[3] = 6;
  77.     game[4] = 57;
  78.     game[5] = 10;
  79.     result = jumpIt(game, i, count);
  80.     printf("This is the result: %d", result);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement