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

Assignment No 1

Uploaded by

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

Assignment No 1

Uploaded by

Umama Abid
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment no 1

SUBMITTED BY: UMAMA ABID


REGISTRATION NO: SP23-BSE-160
SUBMITTED TO: MA’AM HUMERA NIAZ
QUESTION NO 2:
WHAT IS ‘N QUEEN’ PROBLEM:
An NxN chessboard is provided, and the goal of the N-Queen problem is to arrange N queens on
it so that no two queens may attack another. If another queen is positioned in the path of the first,
it will attack it from the horizontal, vertical, or diagonal directions. Backtracking is the most
widely used method for resolving the N Queen puzzle.
ALGORITHM FOR ‘N QUEEN’ PROBLEM

STEPS OF THE ALGORITHM:


 Input
The user inputs the number of queens to be placed on the chessboard.
 solveQueenFunction:
This function will have parameters like board (2D Array), col (Current column to place
the queen), and n (Size of the chessboard).
 IsSafe Function:
Checks if it is safe to place the queen on the board by surety that there is no queen in the
same column, row and diagonals.
 solveNQueen Function:
 Base case: Return true if all queens are positioned (col >= n).
 In the current column, attempt to position a queen for every row:
1. Use the isSafe function to determine whether placing the queen at (i, col) is
safe.
2. Put the queen at (i, col) (set board[i][col] to 1) if it's safe to do so, then call
solveNQueens recursively for the following column (col + 1).
3. Return true if the recursive call returns true (a solution was discovered).
4. If placing the queen at (i, col) does not yield a solution, go back and try the
following row after removing the queen from (i, col).
Return false if there isn't a queen that can be added to the current column.
 printBoard Function:
o This function prints the chessboard's ultimate queen configuration.
o Printing 'Q' indicates each cell that has a queen, whereas 'X' indicates empty cells.
 Main Function:
o reads the user's input value for n.
o Sets all of the cells on an 8x8 chessboard (board) to 0.
o To solve the N-Queens problem, call the solveNQueens function.
o If a solution is found, the printBoard function is used to print the queen placement
on the chessboard. It prints "Solution does not exist" if there isn't a solution.

You might also like