0% found this document useful (0 votes)
14 views5 pages

Aoa Exp8 04

The document details an experiment to implement the N Queen Problem using backtracking, which involves placing N queens on an N×N chessboard without them attacking each other. It includes an algorithm, code in C, and a conclusion highlighting the efficiency of backtracking in solving such problems. The worst-case time complexity is O(N!), but backtracking optimizes the solution process.

Uploaded by

advay.234486101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Aoa Exp8 04

The document details an experiment to implement the N Queen Problem using backtracking, which involves placing N queens on an N×N chessboard without them attacking each other. It includes an algorithm, code in C, and a conclusion highlighting the efficiency of backtracking in solving such problems. The worst-case time complexity is O(N!), but backtracking optimizes the solution process.

Uploaded by

advay.234486101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

08

Name : Advay joshi

​ ​ ​ ​ ​ SE2/S2/04

Aim: To implement N Queen Problem using backtracking technique

Theory:

The N Queen is the problem of placing N chess queens on an N×N chessboard so that no
two queens attack each other.

The expected output is in form of a matrix that has ‘Q’s for the blocks where queens are
placed and the empty spaces are represented by ‘.’s . For example, the following is the
output matrix for the above 4 queen solution.

..Q.​
Q...​
...Q​
.Q..
The idea is to place queens one by one in different columns, starting from the leftmost
column. When we place a queen in a column, we check for clashes with already placed
queens. In the current column, if we find a row for which there is no clash, we mark this
row and column as part of the solution. If we do not find such a row due to clashes, then
we backtrack and return false.

Algorithm:

1.​ Initialize an N × N board with all zeros.


2.​ Check if safe to place a queen at (row, col).
○​ No queen in the same column or upper diagonals.
3.​ Recursively place queens row by row:
○​ If valid, place a queen and move to the next row.
○​ If stuck, backtrack and try another column.
4.​ If all queens are placed, print the board.
5.​ If no solution, print "No solution exists."
Experiment No. 08

Code :

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

void printBoard(int** board, int N) {

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

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

printf("%c ", board[i][j] == 1 ? 'Q' : '.');

printf("\n");

bool isSafe(int** board, int N, int row, int col) {

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

if (board[i][col] == 1)

return false;

for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {

if (board[i][j] == 1)

return false;

for (int i = row - 1, j = col + 1; i >= 0 && j < N; i--, j++) {

if (board[i][j] == 1)

return false;

}
Experiment No. 08

return true;

bool solveNQueens(int** board, int N, int row) {

if (row >= N)

return true;

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

if (isSafe(board, N, row, col)) {

board[row][col] = 1;

if (solveNQueens(board, N, row + 1))

return true;

board[row][col] = 0;

return false;

void solveNQueen(int N) {

int** board = (int**)malloc(N * sizeof(int*));

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

board[i] = (int*)malloc(N * sizeof(int));

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

board[i][j] = 0;

if (solveNQueens(board, N, 0))

printBoard(board, N);

else
Experiment No. 08

printf("No solution exists for N = %d\n", N);

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

free(board[i]);

free(board);

int main() {

int N;

printf("Enter the value of N for the N-Queens problem: ");

scanf("%d", &N);

if (N <= 0) {

printf("Please enter a positive integer for N.\n");

return 1;

solveNQueen(N);

return 0;

Output:
Experiment No. 08

Conclusion:

The N-Queen problem is best solved using backtracking, which tries placing queens one
by one and goes back (backtracks) if a placement is not valid.

●​ Backtracking saves time by avoiding unnecessary checks.


●​ The worst-case time complexity is O(N!), but pruning makes it faster in practice.
●​ It is useful for solving problems like Sudoku, Graph Coloring, and Pathfinding.
●​ Though not the fastest method for all cases, it is a smart way to explore solutions
efficiently.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy