//
// main.cpp
// N Queen
//
// Created by Himanshu on 22/04/22.
//
#include <iostream>
#include <cstdio>
#define N 8
// This function prints the solution
void printSolution(int board[N][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf(" %d ", board[i][j]);
}
printf("\n");
}
}
// A function to check if a queen can
// be placed on board[row][col].
bool check(int board[N][N], int row, int col) {
int i, j;
// Check this row on left side
for (i = 0; i < col; i++) {
if (board[row][i]) {
return false;
}
}
// Check upper diagonal on left side
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j]) {
return false;
}
}
// Check lower diagonal on left side
for (i = row, j = col; j >= 0 && i < N; i++, j--) {
if (board[i][j]) {
return false;
}
}
return true;
}
// A recursive function to solve N Queen problem
bool solveUtil(int board[N][N], int col)
{
// Base case: If all queens are placed then return true
if (col >= N) {
return true;
}
for (int i = 0; i < N; i++) {
// Check if the queen can be placed
if (check(board, i, col)) {
board[i][col] = 1;
// recur to place rest of the queens
if (solveUtil(board, col + 1)) {
return true;
}
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
int main() {
int board[N][N] = {0};
if (solveUtil(board, 0) == false) {
printf("Solution does not exist");
} else {
printf("Solution:\n");
printSolution(board);
}
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBOIFF1ZWVuCi8vCi8vICBDcmVhdGVkIGJ5IEhpbWFuc2h1IG9uIDIyLzA0LzIyLgovLwoKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojZGVmaW5lIE4gOAoKCi8vIFRoaXMgZnVuY3Rpb24gcHJpbnRzIHRoZSBzb2x1dGlvbgp2b2lkIHByaW50U29sdXRpb24oaW50IGJvYXJkW05dW05dKSB7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgTjsgaisrKSB7CiAgICAgICAgICAgIHByaW50ZigiICVkICIsIGJvYXJkW2ldW2pdKTsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgfQp9CgovLyBBIGZ1bmN0aW9uIHRvIGNoZWNrIGlmIGEgcXVlZW4gY2FuCi8vIGJlIHBsYWNlZCBvbiBib2FyZFtyb3ddW2NvbF0uCmJvb2wgY2hlY2soaW50IGJvYXJkW05dW05dLCBpbnQgcm93LCBpbnQgY29sKSB7CiAgICBpbnQgaSwgajsKCiAgICAvLyBDaGVjayB0aGlzIHJvdyBvbiBsZWZ0IHNpZGUKICAgIGZvciAoaSA9IDA7IGkgPCBjb2w7IGkrKykgewogICAgICAgIGlmIChib2FyZFtyb3ddW2ldKSB7CiAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIC8vIENoZWNrIHVwcGVyIGRpYWdvbmFsIG9uIGxlZnQgc2lkZQogICAgZm9yIChpID0gcm93LCBqID0gY29sOyBpID49IDAgJiYgaiA+PSAwOyBpLS0sIGotLSkgewogICAgICAgIGlmIChib2FyZFtpXVtqXSkgewogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgfQogICAgCiAgICAvLyBDaGVjayBsb3dlciBkaWFnb25hbCBvbiBsZWZ0IHNpZGUKICAgIGZvciAoaSA9IHJvdywgaiA9IGNvbDsgaiA+PSAwICYmIGkgPCBOOyBpKyssIGotLSkgewogICAgICAgIGlmIChib2FyZFtpXVtqXSkgewogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gdHJ1ZTsKfQoKCi8vIEEgcmVjdXJzaXZlIGZ1bmN0aW9uIHRvIHNvbHZlIE4gUXVlZW4gcHJvYmxlbQpib29sIHNvbHZlVXRpbChpbnQgYm9hcmRbTl1bTl0sIGludCBjb2wpCnsKICAgIC8vIEJhc2UgY2FzZTogSWYgYWxsIHF1ZWVucyBhcmUgcGxhY2VkIHRoZW4gcmV0dXJuIHRydWUKICAgIGlmIChjb2wgPj0gTikgewogICAgICAgIHJldHVybiB0cnVlOwogICAgfQogICAgCiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykgewogICAgICAgIAogICAgICAgIC8vIENoZWNrIGlmIHRoZSBxdWVlbiBjYW4gYmUgcGxhY2VkCiAgICAgICAgaWYgKGNoZWNrKGJvYXJkLCBpLCBjb2wpKSB7CiAgICAgICAgICAgIGJvYXJkW2ldW2NvbF0gPSAxOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gcmVjdXIgdG8gcGxhY2UgcmVzdCBvZiB0aGUgcXVlZW5zCiAgICAgICAgICAgIGlmIChzb2x2ZVV0aWwoYm9hcmQsIGNvbCArIDEpKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgYm9hcmRbaV1bY29sXSA9IDA7IC8vIEJBQ0tUUkFDSwogICAgICAgIH0KICAgIH0KICAgIAogICAgcmV0dXJuIGZhbHNlOwp9CgoKaW50IG1haW4oKSB7CiAgICBpbnQgYm9hcmRbTl1bTl0gPSB7MH07CiAgICAKICAgIGlmIChzb2x2ZVV0aWwoYm9hcmQsIDApID09IGZhbHNlKSB7CiAgICAgICAgcHJpbnRmKCJTb2x1dGlvbiBkb2VzIG5vdCBleGlzdCIpOwogICAgfSBlbHNlIHsKICAgICAgICBwcmludGYoIlNvbHV0aW9uOlxuIik7CiAgICAgICAgcHJpbnRTb2x1dGlvbihib2FyZCk7CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9Cg==