Question

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)

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.

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

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. :)

Add a comment
Know the answer?
Add Answer to:
In this puzzle, you can bounce between the two 3’s, but you cannot reach any other...
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
  • C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with...

    C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...

  • you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is...

    you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for solving the puzzle and show the configuration after each move. A majority of the code is written, I need help computing 3 functions in the PuzzleState class from the source code I provided below (see where comments ""TODO"" are). Also is this for Artificial Intelligence Requirements You are to create a program in Python 3 that performs the following:...

  • Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 "array" of squares: int board[ROW][COL]={0}; A knight may move perpendicular to the edges o...

    Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 "array" of squares: int board[ROW][COL]={0}; A knight may move perpendicular to the edges of the board, two squares in any of the 4 directions, then one square at right angles to the two-square move. The following lists all 8 possible moves a knight could make from board [3][3]: board[5][4] or board[5][2] or board[4][5] or board[4][1] or board[1][4] or board[1][2] or board[2][5] or board[2][1]...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • Part 3.  Write a program to dynamically allocate some char buffers, use memset to fill them, and...

    Part 3.  Write a program to dynamically allocate some char buffers, use memset to fill them, and use memcpy and memmove to move the data around. I suggest you do part 3a, test and verify it’s working as expected, then add part b, test & verify, then do part 3c.  You only need to turn in the complete program, but doing it in steps will help both with understanding and if you have bugs along the way. Part 3a. a.Allocate 2 char...

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • Java question for two classes: Given the upper limit n as a parameter, the result of...

    Java question for two classes: Given the upper limit n as a parameter, the result of both methods is a boolean[] array of n elements that reveals the answers to that problem for all natural numbers below n in one swoop. public static boolean[] sumOfTwoDistinctSquares(int n) Determines which natural numbers can be expressed in the form a 2 + b 2 so that a and b are two distinct positive integers. In the boolean array returned as result, the i...

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