0% found this document useful (0 votes)
2 views

AOA9

The document contains a C program that implements the N-Queens problem using backtracking. It defines functions to check if a queen can be placed safely on the board, solve the problem recursively, and print the solution. The main function initiates the solving process and displays the solution if it exists.

Uploaded by

vaishnavij840
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

AOA9

The document contains a C program that implements the N-Queens problem using backtracking. It defines functions to check if a queen can be placed safely on the board, solve the problem recursively, and print the solution. The main function initiates the solving process and displays the solution if it exists.

Uploaded by

vaishnavij840
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT NO : 9

CODE :
#include <stdio.h>

#include <stdbool.h>

#define N 8

void print_solution(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");

bool is_safe(int board[N][N], int row, int col) {

int i, j;

// Check this row on the left side

for (i = 0; i < col; i++) {

if (board[row][i])

return false;

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;

bool solve_n_queens_util(int board[N][N], int col) {

if (col >= N)

return true;

for (int i = 0; i < N; i++) {

if (is_safe(board, i, col)) {

board[i][col] = 1;

if (solve_n_queens_util(board, col + 1))

return true;

board[i][col] = 0; // backtrack

return false;

bool solve_n_queens() {

int board[N][N] = { {0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0} };

if (solve_n_queens_util(board, 0) == false) {

printf("Solution does not exist");

return false;

printf("Solution exists:\n");

print_solution(board);

return true;
}

int main() {

solve_n_queens();

return 0;

OUTPUT :

You might also like