Hi, This question is from stanford computer science from web and I thought it wi
ID: 3528788 • Letter: H
Question
Hi, This question is from stanford computer science from web and I thought it will be a good puzzle practice for coding and self learning in c++. I think I will need to use a marker to find the exit to 0. how do I use a marker for this type of puzzle? Any suggestions? Problem 4: Recursive procedures (15 points) Imagine that you have been given a puzzle consisting of a row of squares each of which contains an integer, like this: 3 6 4 1 3 4 2 5 3 0 The circle on the initial square is a marker that can move to other squares along the row. At each step in the puzzle, you may move the marker the number of squares indicated by the integer in the square it currently occupies. The marker may move either left or right along the row but may not move past either end. For example, the only legal first move is to move the marker three squares to the right because there is no room to move three spaces to the left. The goal of the puzzle is to move the marker to the 0 at the far end of the row. In this configuration, you can solve the puzzle by making moves shown in Figure 1 at the top of the next page. Even though this puzzle is solvableExplanation / Answer
Figure 1. Steps in the solution of the puzzle 3 6 4 1 3 4 2 5 3 0 3 6 4 1 3 4 2 5 3 0 3 6 4 1 3 4 2 5 3 0 3 6 4 1 3 4 2 5 3 0 3 6 4 1 3 4 2 5 3 0 3 6 4 1 3 4 2 5 3 0 3 6 4 1 3 4 2 5 3 0 Starting position Step 1: Move right Step 2: Move left Step 3: Move right Step 4: Move right Step 5: Move left Step 6: Move right Write a function bool IsSolvable(int start, Vector & squares); that takes a starting position (which is initially 0 to indicate the beginning of the array) along with the vector of squares and its effective size. The function should return true if it is possible to solve the puzzle from the starting configuration and false if it is impossible. For example, if puzzle1 is a Vector containing the values 3, 6, 4, 1, 3, 4, 2, 5, 3, 0 as shown in the initial diagram, calling IsSolvable(0, puzzle1) should return true. If, however, puzzle2 contains the values 3, 1, 2, 3, 0 as shown in the most recent diagram, calling IsSolvable(0, puzzle2) would return FALSE. In solving this problem, you should keep the following points in mind:Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.