Last active
November 23, 2023 10:01
-
-
Save juanfal/9c722f7ca79a44e778a74a63395ef04e to your computer and use it in GitHub Desktop.
Eight queens problem checking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// t13e13.queens.cpp | |
// juanfc 2023-11-23 | |
// https://gist.github.com/9c722f7ca79a44e778a74a63395ef04e | |
#include <iostream> | |
#include <array> | |
using namespace std; | |
const int N = 8; | |
const char X = '#'; | |
const int NQUEENS = N; | |
typedef array<array<char,N>,N> TChessboard; | |
int main() | |
{ | |
void test(string); | |
test( | |
"# " // 0 | |
" # " // 1 | |
" # " // 2 | |
" #" // 3 | |
" # " // 4 | |
" # " // 5 | |
" # " // 6 | |
" # " // 7 | |
// 01234567 | |
); | |
test( | |
" # " // 0 | |
" # " // 1 | |
" #" // 2 | |
"# " // 3 | |
" # " // 4 | |
" # " // 5 | |
" # " // 6 | |
" # " // 7 | |
// 01234567 | |
); | |
test( | |
" # " // 0 | |
" # " // 1 | |
" #" // 2 | |
"# " // 3 | |
" # " // 4 | |
" # " // 5 | |
" # " // 6 | |
" # " // 7 | |
// 01234567 | |
); | |
return 0; | |
} | |
bool noThreat(TChessboard m) | |
{ | |
bool checkCell(TChessboard m, int row, int col); | |
int cnt = 0; | |
bool ok = true; | |
int i = 0; | |
while (i < N and cnt <= NQUEENS and ok) { | |
int j = 0; | |
while (j < N and cnt <= NQUEENS and ok) { | |
if (m[i][j] == X) { | |
ok = checkCell(m, i, j); | |
} | |
++j; | |
} | |
++i; | |
} | |
return ok; | |
} | |
bool checkCell(TChessboard m, int row, int col) | |
{ | |
bool ok = true; | |
// check column | |
int x = 0; | |
while (x < N and (x == row or m[x][col] != X)) | |
++x; | |
ok = x == N; | |
// check row | |
x = 0; | |
while (ok and x < N and (x == col or m[row][x] != X)) | |
++x; | |
ok = x == N; | |
// cout << row << ", " << col << endl; | |
if (ok) { | |
// \ diag zone right | |
if (row<=col) { | |
x = 0; | |
while (col-row+x < N and (x == row or m[x][col-row+x] != X)) { | |
// cout << x << ", " << col-row+x << endl; | |
++x; | |
} | |
ok = col-row+x == N; | |
} else{ | |
// \ diag zone left | |
x = 0; | |
while (row-col+x < N and (x == col or m[row-col+x][x] != X)) { | |
// cout << row-col+x << ", " << x << endl; | |
++x; | |
} | |
ok = row-col+x == N; | |
} | |
} | |
if (ok) { | |
// / upper | |
if (row+col<N) { | |
x = 0; | |
while (col+row-x >= 0 and (x == col or m[col+row-x][x] != X)) { | |
// cout << col+row-x << ", " << x << endl; | |
++x; | |
} | |
ok = col+row-x < 0; | |
} else{ | |
// / lower | |
x = 0; | |
while (col-(N-1-row)+x < N and (N-1-x == row or m[N-1-x][col-(N-1-row)+x] != X)) { | |
// cout << N-1-x << ", " << col-(N-1-row)+x << endl; | |
++x; | |
} | |
ok = col-(N-1-row)+x == N; | |
} | |
} | |
return ok; | |
} | |
namespace std { | |
template <typename _CharT, typename _Traits> | |
inline basic_ostream<_CharT, _Traits> & | |
tab(basic_ostream<_CharT, _Traits> &__os) { | |
return __os.put(__os.widen('\t')); | |
} | |
} | |
void test(string board) | |
{ | |
bool noThreat(TChessboard); | |
TChessboard fillIt(string); | |
void print(TChessboard); | |
TChessboard r = fillIt(board); | |
print(r); | |
if (noThreat(r)) cout << tab << "CORRECT" << endl; | |
else cout << tab << "INCORRECT" << endl; | |
} | |
TChessboard fillIt(string s) | |
{ | |
TChessboard r; | |
for (int i = 0; i < N; ++i) | |
for (int j = 0; j < N; ++j) | |
r[i][j] = s[i*N+j]; | |
return r; | |
} | |
void print(TChessboard m) | |
{ | |
cout << " "; | |
for (int i = 0; i < N; ++i) | |
cout << i; | |
cout << endl; | |
for (int i = 0; i < N; ++i) { | |
cout << i; | |
for (int j = 0; j < N; ++j) | |
cout << m[i][j]; | |
cout << endl; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment