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

F AI LAB 3 8queens

The document describes the 8-Queens problem, which involves placing eight queens on an 8x8 chessboard so that no two queens threaten each other. It provides pseudocode for a backtracking algorithm that attempts to solve the problem by placing queens column by column and checking for safe positions. The 'isSafe' function ensures that no queens are in the same row, diagonal, or anti-diagonal, and the document notes that the pseudocode may need adaptation for specific implementations.
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)
15 views3 pages

F AI LAB 3 8queens

The document describes the 8-Queens problem, which involves placing eight queens on an 8x8 chessboard so that no two queens threaten each other. It provides pseudocode for a backtracking algorithm that attempts to solve the problem by placing queens column by column and checking for safe positions. The 'isSafe' function ensures that no queens are in the same row, diagonal, or anti-diagonal, and the document notes that the pseudocode may need adaptation for specific implementations.
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

3) Solve 8-Queens Problem with suitable assumptions

BRIEF DESCRIPTION:

The 8 puzzle consists of eight numbered, movable tiles set in a 3x3 frame. One cell of the frame
is always empty thus making it possible to move an adjacent numbered tile into the empty cell.
Such a puzzle is illustrated in following diagram.

For the 8-puzzle, a straight forward description is a 3X3 array of matrix of numbers. Initial
global database is this description of the initial problem state. Virtually any kind of data
structure can be used to describe states. A move transforms one problem state into another state.

N = 8 # (size of the chessboard)

def solveNQueens(board, col):


if col == N:
print(board)
return True
for i in range(N):
if isSafe(board, i, col):
board[i][col] = 1
if solveNQueens(board, col + 1):
return True
board[i][col] = 0
return False

def isSafe(board, row, col):


for x in range(col):
if board[row][x] == 1:
return False
for x, y in zip(range(row, -1, -1), range(col, -1, -1)):
if board[x][y] == 1:
return False
for x, y in zip(range(row, N, 1), range(col, -1, -1)):
if board[x][y] == 1:
return False
return True

board = [[0 for x in range(N)] for y in range(N)]


if not solveNQueens(board, 0):
print("No solution found")

OUTPUT:

The eight queens problem is the problem of placing eight queens on an


8×8 chessboard such that none of them attack one another (no two are in
the same row, column, or diagonal). More generally, the n queens
problem places n queens on an n×n chessboard. There are different
solutions for the problem. Backtracking | Set 3 (N Queen
Problem) Branch and Bound | Set 5 (N Queen Problem) You can find
detailed solutions
at http://en.literateprograms.org/Eight_queens_puzzle_(C)
Explanation:
• This pseudocode uses a backtracking algorithm to find a
solution to the 8 Queen problem, which consists of placing 8
queens on a chessboard in such a way that no two queens
threaten each other.
• The algorithm starts by placing a queen on the first column,
then it proceeds to the next column and places a queen in
the first safe row of that column.
• If the algorithm reaches the 8th column and all queens are
placed in a safe position, it prints the board and returns true.
If the algorithm is unable to place a queen in a safe position in a
certain column, it backtracks to the previous column and tries a
different row.
• The “isSafe” function checks if it is safe to place a queen on a
certain row and column by checking if there are any queens in
the same row, diagonal or anti-diagonal.
• It’s worth to notice that this is just a high-level pseudocode and
it might need to be adapted depending on the specific
implementation and language you are using.
Here is an example of pseudocode for solving the 8 Queen problem
using backtracking:
Time Complexity : O((m + q) log^2 n)
Space Complexity : O((m + q) log n)

You might also like