From 57f5f47439098a4b9b78fa21ed4d96ec426d9cab Mon Sep 17 00:00:00 2001 From: Ronald Juarez Date: Sat, 8 Jun 2019 16:16:24 -0700 Subject: [PATCH 1/3] adding ch12-8 eight queens problem for cpp" ; --- .../8-12-Eight-Queens.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 chapter-8-recursion-and-Dynamic-Programming/8-12-Eight-Queens.cpp 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 From 5096d1ae5beb79e4de8500d3818261ad6a64a720 Mon Sep 17 00:00:00 2001 From: Ronald Juarez Date: Sat, 8 Jun 2019 16:17:16 -0700 Subject: [PATCH 2/3] Revert "adding ch12-8 eight queens problem for cpp"" This reverts commit 57f5f47439098a4b9b78fa21ed4d96ec426d9cab. --- .../8-12-Eight-Queens.cpp | 81 ------------------- 1 file changed, 81 deletions(-) delete mode 100644 chapter-8-recursion-and-Dynamic-Programming/8-12-Eight-Queens.cpp 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 deleted file mode 100644 index c58fae9..0000000 --- a/chapter-8-recursion-and-Dynamic-Programming/8-12-Eight-Queens.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#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 From df5222bfd50508101d7e7847f5c8ff68beb68249 Mon Sep 17 00:00:00 2001 From: Ronald Juarez Date: Sat, 8 Jun 2019 16:17:42 -0700 Subject: [PATCH 3/3] Revert "Revert "adding ch12-8 eight queens problem for cpp""" This reverts commit 5096d1ae5beb79e4de8500d3818261ad6a64a720. --- .../8-12-Eight-Queens.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 chapter-8-recursion-and-Dynamic-Programming/8-12-Eight-Queens.cpp 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