Last active
November 9, 2018 16:30
-
-
Save heatblazer/c36af8eeaebe1137eb6ccd24dd70b883 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
template <int N> struct SolveQ | |
{ | |
unsigned char mat[N][N]; | |
SolveQ() | |
{ | |
for(int i=0; i < N; ++i) | |
{ | |
for(int j=0; j < N; ++j) | |
{ | |
mat[i][j] = 'O'; | |
} | |
} | |
} | |
void fillDiag(int x, int y, unsigned char d) | |
{ | |
for (int i=x, j= y; i >= 0 && j < N; i--, j++) | |
mat[i][j] = d; | |
for (int i=x, j= y; i < N && j >= 0; i++, j--) | |
mat[i][j] = d; | |
for(int i=x, j=y; i < N && j < N; i++, j++) | |
mat[i][j] = d; | |
for(int i=x, j=y; i >=0 && j >= 0; i--, j--) | |
mat[i][j] = d; | |
for(int i=0; i < x; ++i) | |
mat[i][y] = d; | |
for(int i=x; i < N; ++i) | |
mat[i][y] = d; | |
for(int i=0; i < y; ++i) | |
mat[x][i] = d; | |
for(int i=y; i < N; ++i) | |
mat[x][i] = d; | |
} | |
bool ocupied(int x, int y, unsigned char data) | |
{ | |
return mat[x][y] == data; | |
} | |
void solveHelper(int x, int y, unsigned char data) | |
{ | |
bool isOk = true; | |
int safex, safey ; | |
for (int i=x, j= y; i >= 0 && j < N; i--, j++) | |
if (!ocupied(i, j, 'X')) | |
{ | |
safex = i; | |
safey = j; | |
} | |
//std::cout << "Safe place found at: ["<<i<<"]["<<j<<"]\r\n"; | |
else | |
{ | |
isOk = false; | |
break; | |
} | |
for (int i=x, j= y; i < N && j >= 0; i++, j--) | |
if (!ocupied(i, j, 'X')) | |
std::cout << "Safe place found at: ["<<i<<"]["<<j<<"]\r\n"; | |
else | |
{ | |
isOk = false; | |
break; | |
} | |
for(int i=x, j=y; i < N && j < N; i++, j++) | |
if (!ocupied(i, j, 'X')) | |
std::cout << "Safe place found at: ["<<i<<"]["<<j<<"]\r\n"; | |
else | |
{ | |
isOk = false; | |
break; | |
} | |
for(int i=x, j=y; i >=0 && j >= 0; i--, j--) | |
if (!ocupied(i, j, 'X')) | |
std::cout << "Safe place found at: ["<<i<<"]["<<j<<"]\r\n"; | |
else | |
{ | |
isOk = false; | |
break; | |
} | |
for(int i=0; i < x; ++i) | |
if (!ocupied(i, y, 'X')) | |
std::cout << "Safe place found at: ["<<i<<"]["<<y<<"]\r\n"; | |
else | |
{ | |
isOk = false; | |
break; | |
} | |
for(int i=x; i < N; ++i) | |
if (!ocupied(i, y, 'X')) | |
std::cout << "Safe place found at: ["<<i<<"]["<<y<<"]\r\n"; | |
else | |
{ | |
isOk = false; | |
break; | |
} | |
for(int i=0; i < y; ++i) | |
if (!ocupied(x, i, 'X')) | |
std::cout << "Safe place found at: ["<<x<<"]["<<i<<"]\r\n"; | |
else | |
{ | |
isOk = false; | |
break; | |
} | |
for(int i=y; i < N; ++i) | |
if (!ocupied(x, i, 'X')) | |
std::cout << "Safe place found at: ["<<x<<"]["<<i<<"]\r\n"; | |
else | |
{ | |
isOk = false; | |
break; | |
} | |
if (isOk) | |
std::cout << "Position ["<<x<<"][" << y<<"] is OK\r\n"; | |
else | |
std::cout << "Position ["<<x<<"][" << y<<"] is NOT OK\r\n"; | |
} | |
void solve() | |
{ | |
for(int i=0; i < N; ++i) | |
for(int j=0; j < N; ++j) | |
solveHelper(i, j, 'X'); | |
} | |
void print() | |
{ | |
for(int i=0; i < N; ++i) | |
{ | |
for(int j=0; j < N; ++j) | |
{ | |
std::cout << " [" << mat[i][j] << "] "; | |
} | |
std::cout << "\r\n\r\n"; | |
} | |
} | |
}; | |
int main() | |
{ | |
SolveQ<8> s; | |
s.fillDiag(4, 0, 'X'); | |
s.solve(); | |
s.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment