Skip to content
Snippets Groups Projects
Commit e7a03a3a authored by Marcus M. Darden's avatar Marcus M. Darden
Browse files

Initial commit.

Add files from sample and basic README.
parent ed4d72b1
No related branches found
No related tags found
No related merge requests found
// Example of solution to N-Queens problem
// Dr. David Paoletti
#include <iostream>
#include <string>
#include "NQueens.hpp"
using namespace std;
const bool NQueens::AVAILABLE = true;
// Construct the "available"
NQueens::NQueens(unsigned n) : squares{n}, solutions{0}, tried{0},
positionInRow(n, -1), column(n, AVAILABLE),
leftDiagonal(2 * n - 1, AVAILABLE), rightDiagonal(2 * n - 1, AVAILABLE) {
} // NQueens constructor
// Solve this board (public member function)
// If display == true, display each board as a solution is found
void NQueens::solve(bool display) {
putQueen(0, display);
} // NQueens::solve()
// Place a queen in row
// If display == true, display each board as a solution is found
void NQueens::putQueen(unsigned row, bool display) {
// Check every column within this row
for (unsigned col = 0; col < squares; col++) {
// Check if proposed placement is promising
if ( column[col] == AVAILABLE
&& leftDiagonal[row + col] == AVAILABLE
&& rightDiagonal[row - col + (squares - 1)] == AVAILABLE) {
positionInRow[row] = col;
column[col] = !AVAILABLE;
leftDiagonal[row + col] = !AVAILABLE;
rightDiagonal[row - col + (squares - 1)] = !AVAILABLE;
// Check for solution
if (row == squares - 1) {
solutions++;
if (display)
{
cout << "Solution found:\n";
displayBoard();
} // if
} // if
else
putQueen(row + 1, display);
// Undo this move and thus backtrack
column[col] = AVAILABLE;
leftDiagonal[row + col] = AVAILABLE;
rightDiagonal[row - col + (squares - 1)] = AVAILABLE;
} // if
tried++;
} // for
} // NQueens::putQueen()
// Send the board to the screen
void NQueens::displayBoard() {
string bar = "+";
for (unsigned col = 0; col < squares; col++)
bar += "---+";
for (unsigned row = 0; row < squares; row++) {
cout << " " << bar << endl << " ";
for (unsigned col = 0; col < squares; col++) {
cout << " | ";
if (positionInRow[row] == col)
cout << "Q";
else
cout << " ";
} // for col
cout << " |" << endl;
} // for row
cout << " " << bar << endl;
cout << endl;
} // NQueens::displayBoard()
\ No newline at end of file
// Example of solution to N-Queens problem
// Dr. David Paoletti
#ifndef NQUEENS_HPP
#define NQUEENS_HPP
#include <vector>
using namespace std;
class NQueens {
private:
unsigned squares;
unsigned long long solutions, tried;
vector<int> positionInRow;
vector<bool> column, leftDiagonal, rightDiagonal;
static const bool AVAILABLE;
void putQueen(unsigned row, bool display = true);
void displayBoard();
public:
NQueens(unsigned n);
void solve(bool display = true);
unsigned long long getSolutions() const { return solutions; }
unsigned long long getTried() const { return tried; }
}; // NQueens
#endif // NQUEENS_HPP
\ No newline at end of file
// Example of solution to N-Queens problem
// Dr. David Paoletti
#include <iostream>
#include <cmath>
#include "NQueens.hpp"
using namespace std;
int main() {
unsigned n;
char display;
double perms, savings;
cout << "Enter number of queens: ";
cin >> n;
cout << "Display results? ";
cin >> display;
display = toupper(display);
NQueens board(n);
board.solve(display == 'Y');
perms = pow(n, n);
savings = (1 - (double)board.getTried() / perms) * 100;
cout << "There were " << board.getSolutions() << " solutions\n";
cout << board.getTried() << " branches were explored out of ";
cout << perms << " possible permutations,\n";
cout << "A savings of " << savings << "%\n";
return 0;
} // main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment