0% found this document useful (0 votes)
55 views3 pages

Lab12-N Queen

The document outlines a C/C++ program designed to solve the N Queen's problem using a backtracking algorithm. It details the algorithm steps, provides the complete code for implementation, and includes an example input and output for N=4. The conclusion states that the program executed successfully.

Uploaded by

ananya.r.amcec
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)
55 views3 pages

Lab12-N Queen

The document outlines a C/C++ program designed to solve the N Queen's problem using a backtracking algorithm. It details the algorithm steps, provides the complete code for implementation, and includes an example input and output for N=4. The conclusion states that the program executed successfully.

Uploaded by

ananya.r.amcec
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/ 3

Design and implement C/C++ Program for N Queen's problem using Backtracking.. .

Aim: To solve N Queen's problem using Backtracking


Algorithm:
 Place queens row by row.
 For each row, try placing a queen in each column.
 If it's safe (no conflict with previous queens), place it and move to the next row.
 If placing the queen leads to a solution, return True.
 If not, backtrack: remove the queen and try the next column.
 Repeat until all queens are placed or all options are exhausted.

Program:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Function to print the solution
void printSolution(int **board, int N)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
printf("%s ", board[i][j] ? "Q" : "#");
}
printf("\n");
}
}
// Function to check if a queen can be placed on board[row][col]
bool isSafe(int **board, int 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 utility function to solve N Queen problem
bool solveNQUtil(int **board, int N, int col)
{
// If all queens are placed, then return true
if (col >= N)
{
return true;
}
// Consider this column and try placing this queen in all rows one by one
for (int i = 0; i < N; i++)
{
if (isSafe(board, N, i, col))
{
// Place this queen in board[i][col]
board[i][col] = 1;
// Recur to place rest of the queens
if (solveNQUtil(board, N, col + 1))
{
return true;
}
// If placing queen in board[i][col] doesn't lead to a solution,
// then remove queen from board[i][col]
board[i][col] = 0; // BACKTRACK
}
}
// If the queen cannot be placed in any row in this column col, then return false
return false;
}
bool solveNQ(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 (!solveNQUtil(board, N, 0))
{
printf("Solution does not exist\n");
for (int i = 0; i < N; i++)
{
free(board[i]);
}
free(board);
return false;
}
printSolution(board, N);
for (int i = 0; i < N; i++)
{
free(board[i]);
}
free(board);
return true;
}
int main()
{
int N;
printf("Enter the number of queens: ");
scanf("%d", &N);
solveNQ(N);
return 0;
}
Input:
Enter the number of queens: 4
Output:
##Q#
Q###
###Q
#Q##

Conclusion:
Thus the program was executed sucessfully

You might also like