#include <iostream>
const int N = 4;
void printSolution(int board[N][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
std::cout << board[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
bool isSafe(int board[N][N], int row, int col) {
for (int i = 0; i < row; i++) {
if (board[i][col] == 1) {
return false;
}
}
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 1) {
return false;
}
}
for (int i = row, j = col; i >= 0 && j < N; i--, j++) {
if (board[i][j] == 1) {
return false;
}
}
return true;
}
bool solveNQueensUtil(int board[N][N], int row) {
if (row == N) {
printSolution(board);
return true;
}
bool res = false;
for (int i = 0; i < N; i++) {
if (isSafe(board, row, i)) {
board[row][i] = 1;
res = solveNQueensUtil(board, row + 1) || res;
board[row][i] = 0;
}
}
return res;
}
void solveNQueens() {
int board[N][N] = {0};
if (!solveNQueensUtil(board, 0)) {
std::cout << "There is no solution" << std::endl;
}
}
int main() {
solveNQueens();
return 0;
}