Question: The Program should output 92 solutions as 1D Array Q[r] with r being the row and Q[r] being the colomn of the queen, all code
The Program should output 92 solutions as 1D Array Q[r] with r being the row and Q[r] being the colomn of the queen, all code provided by the professor can not be changed or removed from the program it must be entirely based off of it the only thing that can be changed is the functions inside the classes.
The eight queens puzzle is the problem of placing eight chess queens on an 88 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. //============================================================================== // Absract Base Class // // Possible actions are numbered 0 to action_count - 1 // Method action(int act_num) tries to execute action number act_num on the // current puzzle state. // returns true if action succeeded, false otherwise // class Puzzle { public: int action_count; // number of possible actions virtual bool action(int act_num) = 0; // try to take action act_num virtual void undo_last_action() = 0; virtual bool goal() = 0; // true if puzzle is in a goal state virtual void print_solution() = 0; }; //============================================================================== // 8 Queens class // // Programming Assignment: Implement this class // class Queen8: public Puzzle { public: Queen8(); bool action(int act_num); void undo_last_action(); bool goal(); void print_solution(); private: int Q[8]; // Q[r] = column number of queen in row r int queen_count; // number of queens successfully placed on the board bool safe(int col); // true if next queen can go in column number col }; //============================================================================== // Recursive Backtracking Puzzle Solver // void solve(Puzzle &P) { static int solution_count = 0; if(P.goal()) { cout << " Solution " << ++solution_count << " "; P.print_solution(); system("pause"); return; } for (int a = 0; a < P.action_count; a++) // try all possible moves if(P.action(a)) // the move succeeded { solve(P); P.undo_last_action(); // backup to try other options } } //============================================================================== // int main() { Puzzle Queen8; solve(P); cout << " No more solutions "; system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
