diff --git a/chapter-8-recursion-and-Dynamic-Programming/8-12-Eight-Queens.cpp b/chapter-8-recursion-and-Dynamic-Programming/8-12-Eight-Queens.cpp new file mode 100644 index 0000000..c58fae9 --- /dev/null +++ b/chapter-8-recursion-and-Dynamic-Programming/8-12-Eight-Queens.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +using namespace std; + +int GRID_SIZE = 8; + +bool checkValid(vector columns, int row1, int col1) { + + for (int row2 = 0; row2 < row1; row2++) { + int col2 = columns[row2]; + + // check if they are in the same column + // we don't need to check rows since always row2 < row1 + if (col1 == col2) { + return false; + } + + // check if they are in the same diaganol + if (abs(col2 - col1) == abs(row1 - row2)) { + return false; + } + } + + return true; +} + +void placeQueens(int row, vector &columns, vector > &results) { + if (row == GRID_SIZE) { + results.push_back(columns); + } + else { + for (int col = 0; col < GRID_SIZE; col++) { + if (checkValid(columns, row, col)) { + columns[row] = col; + placeQueens(row + 1, columns, results); + } + } + + } +} + +void drawLine() { + for (int i = 0; i < GRID_SIZE * 2 + 1; i++) + cout << '-'; + cout << endl; +} + +void printBoard(vector columns) { + drawLine(); + for (int i = 0; i < GRID_SIZE; i++) { + cout << "|" ; + for (int j = 0; j < GRID_SIZE; j++) { + if (columns[i] == j) { + cout << "Q|" ; + } + else { + cout << " |"; + } + } + cout << endl; + drawLine(); + } + cout << endl; +} + +void printBoards(vector> columns) { + int numSolutions = columns.size(); + for (int i = 0; i < numSolutions; i++) { + printBoard(columns[i]); + } +} + +int main() { + vector columns(GRID_SIZE, -1); + vector > results; + placeQueens(0, columns, results); + printBoards(results); + return 0; +} \ No newline at end of file