In this puzzle, you can bounce between the two 3\'s, but you cannot reach any ot
ID: 3688717 • Letter: I
Question
In this puzzle, you can bounce between the two 3's, but you cannot reach any other spaces. Write a program that reads in a file of puzzles of this type and determines if each puzzle is solvable. To do this, write a function bool Solvable(int position, int squaret[], Int nSquares) that takes the current position of the marker along with the array of spaces and the puzzle's effective size. The function should return true if it is possible to sohw the puzzle from the starting configuration and false if it is impossible. Details: No puzzle will contain more than 100 possible locations (counting both the beginning and the ending positions). Beginning position is always index zero. The final position is the index that stores a zero. It does not have to be at the end of the puzzle. AS your program analyzes the puzzles, write messages to the screen showing the progress. Milmic the file given on Moodle to see what to show. The file contains an unknown number of puzzles. Each puzzle will be on its own line. The first number on each line reveals how many numbers are in the puzzle, the remaining numbers are the puzzle itself. (example: 6 2 4 1 1 2 0).Explanation / Answer
#include <iostream>
#include "random.h"
#include "simpio.h"
#include "map.h"
#include "vector.h"
const bool TEST = true;
const int TestRuns = 100;
const int MinIndex = 40;
const int MaxIndex = 48;
bool Solvable(int start, Vector<int> & squares);
int main()
{
Randomize();
int size,c=0;
int runs = TestRuns;
int solvable = runs;
while (true)
{
Vector<int> squares;
cout << "Please enter the puzzle size ";
cin>>size;
for(c=0;c<size;c++)
{
cout << "Enter a number between 1 and 9 to seed puzzle (0 to end): ";
int number = GetInteger();
if (number >= 0 && number <= 9)
squares.add(number);
else
{
cout << "Try again..." << endl;
}
if (number == 0) {
break;
}
}//for
}//while
if (!Solvable(0, squares)) {
cout << "NOT ";
solvable--;
}
cout << "SOLVABLE" << endl;
if (TEST)
{
if (runs == 0)
{
cout << "End of Tests: " << solvable << " of " << TestRuns << " SOLVABLE." << endl;
cout << "Continue? (y or n): ";
string line = GetLine();
if (line[0] == 'n') break;
runs = TestRuns;
solvable = runs;
}
}
else {
cout << "Continue? (y or n): ";
string line = GetLine();
if (line[0] == 'n') break;
}
}
return 0;
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.