Lab 3 Exercise 1

Lab 3 Exercise 1

You cannot submit for this problem because the homework's deadline is due.

Ex1. Card Game!

Related Topics: Recursion.

Problem: Let's play with cards! You are given a sequence of n cards, all placed face down, and the number written on the other side of each card is guaranteed to be unique and positive. Each card is also indexed from 0 to n-1, which corresponds to its position in the sequence. Among the cards, there is always one with 280 on it. The rule of the game is stated as follows:

  1. You will be given a start position, namely p. Make its face up.
  2. If the card on that position has value 280, then you win. Otherwise, proceed to step 3.
  3. Suppose the value on the card at this position p is v. Then you can choose either the card on position p+v or position p-v. Once a card is chosen, make its face up. Note that the chosen position must be valid, i.e., you cannot choose a card outside the given sequence. Moreover, you cannot choose a card already face up. If no card can be chosen, you lose.
  4. Once you've picked your card from step 3, update the current position, go to step 2.

Requirement: Please refer to ex1.cpp in the starter_files folder. The skeleton code is already provided. You are required to use recursion to implement the function below, where count is the number of element in the card array, arr[] represents the sequence of values on each card, position is the start position of the game. You may use a helper function if you want.

bool canWin(int count, int arr[], int position) {
    // TODO: implement this function
}

Input Format: There will be 3 lines, the first line only contains the number of cards in the sequence. The second line gives the values of all the cards. The third line specifies the start position (0-index based).
cpp
// sample input
3
280 203 281
0

Output Format: Either 0 (for false) or 1 (for true).
cpp
1

Hint:

  1. We guarantee that the start position is valid.
  2. Once a card has been chosen, you cannot choose it again. Therefore, you should find a way to record this.

Lab 3

Not Claimed
Status
Finished
Problems
3
Open Since
2023-05-28 12:00
DDL
2023-06-04 23:59
Extension
72.0 hour(s)