From 1880376d2285d2094b91c7f5e0d0fb02c9993adc Mon Sep 17 00:00:00 2001 From: dipesh88 <63647097+dipesh88@users.noreply.github.com> Date: Sun, 3 Oct 2021 16:07:25 +0530 Subject: [PATCH 1/3] Kindly add this file --- demo.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 demo.cpp diff --git a/demo.cpp b/demo.cpp new file mode 100644 index 0000000..bf25d88 --- /dev/null +++ b/demo.cpp @@ -0,0 +1,56 @@ +#include + +#include + +using namespace std; + +int main() +{ + + const int DIMEN = 24; + + + char canvas [DIMEN][DIMEN] = {{0}}; + + for(int row = 0; row < DIMEN; row++) + { + + for(int col = 0; col < DIMEN; col++) + { + + int x = col - DIMEN/2; + + int y = DIMEN/2 - row; + + + int sumsq = x*x + y*y; + + if((95 < sumsq) && (sumsq < 105)) + { + + canvas[row][col] = '*'; + + } + + } + + } + + for(int row = 0; row < DIMEN; row++) + { + + for(int col = 0; col < DIMEN; col++) + { + + + printf("%c ", canvas[row][col]); + + } + + cout << endl; + + } + + return 0; + +} From a1a699d1b1f273c8e26747dbccbc0f2bf5967f07 Mon Sep 17 00:00:00 2001 From: dipesh88 <63647097+dipesh88@users.noreply.github.com> Date: Sun, 3 Oct 2021 23:15:41 +0530 Subject: [PATCH 2/3] N_Queen N_Queen --- new.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 new.c diff --git a/new.c b/new.c new file mode 100644 index 0000000..63b9677 --- /dev/null +++ b/new.c @@ -0,0 +1,67 @@ +#include +#include + +#define MAX 10 + +int x[MAX]; + +int Place(int k, int i) +{ + for (int j = 1; j <= k - 1; j++) + { + if (x[j] == i || (abs(x[j] - i) == abs(j - k))) + { + return 0; + } + } + return 1; +} + +void display(int n) +{ + printf("Arrangement\n"); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + if (x[i] != j) + { + printf("."); + } + else + { + printf("Q"); + } + } + printf("\n"); + } +} + +void nqueens(int k, int n) +{ + + for (int i = 1; i <= n; i++) + { + if (Place(k, i)) + { + x[k] = i; + if (k == n) + { + display(n); + } + else + { + nqueens(k + 1, n); + } + } + } +} + +int main() +{ + int n = 0; + printf("Enter the size of board\n"); + scanf("%d", &n); + nqueens(1, n); + return 0; +} \ No newline at end of file From 987ab0fc9b7bf20fd29f83185fc53893d09e2860 Mon Sep 17 00:00:00 2001 From: dipesh88 <63647097+dipesh88@users.noreply.github.com> Date: Sun, 3 Oct 2021 23:19:47 +0530 Subject: [PATCH 3/3] K tour problem algorithm implemented using C K tour problem algorithm implemented using C --- ktour.cpp | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 ktour.cpp diff --git a/ktour.cpp b/ktour.cpp new file mode 100644 index 0000000..c48140a --- /dev/null +++ b/ktour.cpp @@ -0,0 +1,147 @@ + +#include +using namespace std; +int m, n, k; +int count = 0; +void makeBoard(char **board) +{ + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + board[i][j] = '_'; + } + } +} + +void displayBoard(char **board) +{ + cout << endl + << endl; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cout << " " << board[i][j] << " "; + } + cout << endl; + } +} + +void attack(int i, int j, char a, + char **board) +{ + + if ((i + 2) < m && (j - 1) >= 0) + { + board[i + 2][j - 1] = a; + } + if ((i - 2) >= 0 && (j - 1) >= 0) + { + board[i - 2][j - 1] = a; + } + if ((i + 2) < m && (j + 1) < n) + { + board[i + 2][j + 1] = a; + } + if ((i - 2) >= 0 && (j + 1) < n) + { + board[i - 2][j + 1] = a; + } + if ((i + 1) < m && (j + 2) < n) + { + board[i + 1][j + 2] = a; + } + if ((i - 1) >= 0 && (j + 2) < n) + { + board[i - 1][j + 2] = a; + } + if ((i + 1) < m && (j - 2) >= 0) + { + board[i + 1][j - 2] = a; + } + if ((i - 1) >= 0 && (j - 2) >= 0) + { + board[i - 1][j - 2] = a; + } +} + +bool canPlace(int i, int j, char **board) +{ + if (board[i][j] == '_') + return true; + else + return false; +} + +void place(int i, int j, char k, char a, + char **board, char **new_board) +{ + for (int y = 0; y < m; y++) + { + for (int z = 0; z < n; z++) + { + new_board[y][z] = board[y][z]; + } + } + new_board[i][j] = k; + attack(i, j, a, new_board); +} + +void kkn(int k, int sti, int stj, char **board) +{ + if (k == 0) + { + displayBoard(board); + count++; + } + else + { + for (int i = sti; i < m; i++) + { + for (int j = stj; j < n; j++) + { + + if (canPlace(i, j, board)) + { + + char **new_board = new char *[m]; + for (int x = 0; x < m; x++) + { + new_board[x] = new char[n]; + } + place(i, j, 'K', 'A', board, new_board); + + kkn(k - 1, i, j, new_board); + + for (int x = 0; x < m; x++) + { + delete[] new_board[x]; + } + delete[] new_board; + } + } + stj = 0; + } + } +} + +int main() +{ + m = 4, n = 3, k = 6; + + char **board = new char *[m]; + for (int i = 0; i < m; i++) + { + board[i] = new char[n]; + } + + makeBoard(board); + + kkn(k, 0, 0, board); + + cout << endl + << "Total number of solutions : " + << count; + return 0; +}