From 4fdfe9388395abfd73112039420a9bb8d1ecbea9 Mon Sep 17 00:00:00 2001 From: Alfredo Molina Date: Thu, 17 Oct 2019 14:00:32 -0500 Subject: [PATCH 1/2] using a 6x6 board for the N queen problem using bruteforce --- 6 queens brute force algorithm/main.cpp | 126 ++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 6 queens brute force algorithm/main.cpp diff --git a/6 queens brute force algorithm/main.cpp b/6 queens brute force algorithm/main.cpp new file mode 100644 index 0000000..b162f31 --- /dev/null +++ b/6 queens brute force algorithm/main.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +void reinas(); +bool choque(bool tablero[6][6]); + +int main() +{ + system("clear"); + bool tablero[6][6]; + bool res = false; + int cont=0; + + //Create Chessboard + for(int i=0;i<6;i++){ + for(int j=0;j<6;j++){ + tablero[i][j] = false; + } + } + + // Queens movement + for(int a = 0; a < 6; a++){ + for(int b = 0; b < 6; b++){ + for(int c = 0; c < 6; c++){ + for(int d = 0; d < 6; d++){ + for(int e = 0; e < 6; e++){ + for(int f = 0; f < 6; f++){ + tablero[0][a]=true; + tablero[1][b]=true; + tablero[2][c]=true; + tablero[3][d]=true; + tablero[4][e]=true; + tablero[5][f]=true; + res = choque(tablero); + if(res == true){ + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 6; j++) { + if(tablero[i][j] == true){ + cout << " 1 / "; + }else{cout << " 0 / ";} + } + cout << endl; + } + cout << endl; + cont++; + cout << cont; + cout << endl << endl; + } + tablero[0][a]=false; + tablero[1][b]=false; + tablero[2][c]=false; + tablero[3][d]=false; + tablero[4][e]=false; + tablero[5][f]=false; + } + } + } + } + } + } + return 0; +} + +bool choque(bool tablero[6][6]){ + bool atq = true; + for(int i=0;i<6;i++){ + for(int j=0;j<6;j++){ + if(tablero[i][j] == true){ + //Atack in X direction + for(int x=i+1;x<6;x++){ + if(tablero[x][j] == true){ + atq = false; + } + } + for(int x=i-1;x>0;x--){ + if(tablero[x][j] == true){ + atq = false; + } + } + //Atack in Y direction + for(int y=j+1;y<6;y++){ + if(tablero[i][y] == true){ + atq = false; + } + } + for(int y=j-1;y>0;y--){ + if(tablero[i][y] == true){ + atq = false; + } + } + //Atack in DIAGONAL Right up/down + int y=j; + for(int x=i+1;x<6;x++){ + if(y+1 == 6){break;}else{y++;} + if(tablero[x][y] == true){ + atq = false; + } + } + y=j; + for(int x=i+1;x<6;x++){ + if(y-1 == -1){break;}else{y--;} + if(tablero[x][y] == true){ + atq = false; + } + } + //Atack in DIAGONAL Left up/down + y=j; + for(int x=i-1;x>0;x--){ + if(y+1 == 6){break;}else{y++;} + if(tablero[i][y] == true){ + atq = false; + } + } + y=j; + for(int x=i-1;x>0;x--){ + if(y-1 == -1){break;}else{y--;} + if(tablero[i][y] == true){ + atq = false; + } + } + } + } + } + return atq; +} From 2cddad81909cb2a5482af2e5113ad20d4effebd0 Mon Sep 17 00:00:00 2001 From: Alfredo Molina Date: Thu, 17 Oct 2019 14:11:25 -0500 Subject: [PATCH 2/2] created a algorithm to find the determinant in a 3x3 matrix --- Determinat matrix/.DS_Store | Bin 0 -> 6148 bytes Determinat matrix/main.cpp | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Determinat matrix/.DS_Store create mode 100644 Determinat matrix/main.cpp diff --git a/Determinat matrix/.DS_Store b/Determinat matrix/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 + +using namespace std; + +int main() +{ + + int a[3][3]; + det=a[1][1]; + for(k=1;k<=m;k++) + { l=k+1; + for(i=l;i<=n;i++) + { for(j=l;j<=n;j++) + a[i][j] = ( a[k][k]*a[i][j]-a[k][j]*a[i][k] )/a[k][k]; } + det=det*a[k+1][k+1]; + } + cout << endl; + cout << "DETERMINANT = " << det << endl; + cout << "------------------------" << endl; +}