In this puzzle, you can bounce between the two 3’s, but you cannot reach any oth
ID: 3738349 • Letter: I
Question
In this puzzle, you can bounce between the two 3’s, but you cannot reach any other squares. Write a function bool Solvable(int start, int[] squares) that takes a starting position of the marker along with the array of squares. The function should return true if it is possible to solve the puzzle from the starting configuration, and false if it is impossible. You may assume all the integers in the array are positive except for the last entry, the goal square, which is always zero.
Q1. A Recursive Puzzle You have been given a puzzle consisting of a row of squares each containing an integer, like this: 316 41 34 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 the following set of moves: Starting Position: 364 3 4 25 30 Step : (move right) Step 2: (move left) Step 3: (move right) Step 4: (move right) Step 5: (move left) 36 4 36 41 34 2 5 3 0 3 64 3 4 (25 30 36 4 1 3 42 5 3 36 4 3 42 5 3 0 36 41 34 2 5 3 0 Step 6 (move right)Explanation / Answer
Solution:
The function is given below:
Function:
bool Solvable(int start, Vector<int> & squares)
{
Map<bool> visited;
for (int i = 0; i < squares.size(); i++) {
visited.put(IntegerToString(i),false);
}
return RecursiveSolvable(start, squares[], size);
}
bool RecursiveSolvable(int start, Vector<int> & squares,int length)
{
Map<bool> visited;
for (int i = 0; i < squares.size(); i++)
{
visited.put(IntegerToString(i),false);
}
if (squares[start] == 0)
return true;
visited.put(IntegerToString(last),true);
if (visited.get(IntegerToString(start)))
return false;
if (!((start + squares[start]) >= length))
{
return RecursiveSolvable(start + squares[start], squares[],length);
}
if (!((start + squares[start])< 0))
{
return RecursiveSolvable(start - squares[start], squares[],length);
}
return false;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.